Eae pessoal, eu gostaria de criar um “factory” para ler e escrever em um arquivo… Então fiz o seguinte…
[code]import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Arquivo {
private File dicionario;
private FileWriter fw;
private BufferedWriter bw;
private FileReader fr;
private BufferedReader br;
public Arquivo() throws IOException {
this.dicionario = new File("dicionario.dat");
this.fw = new FileWriter(dicionario);
this.bw = new BufferedWriter(fw);
this.fr = new FileReader(dicionario);
this.br = new BufferedReader(fr);
}
public void escreveArquivo(String conteudo) throws IOException{
bw.write(conteudo);
fw.close();
bw.close();
}
public String leArquivo() throws IOException{
String conteudo = "";
String linha = "";
while( ( linha = br.readLine() ) != null ) {
conteudo += linha;
conteudo += "\n";
}
fr.close();
br.close();
return conteudo;
}
}[/code]
E a classe de teste
[code]import java.io.IOException;
public class TestaArquivo {
public static void main(String[] args) throws IOException{
Arquivo arquivo = new Arquivo();
arquivo.escreveArquivo("Joaozinho 30");
System.out.println(arquivo.leArquivo());
}
}[/code]
E o erro (aheuaheuahuehaue)
[quote]Exception in thread “main” java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.close(Unknown Source)
at Arquivo.escreveArquivo(Arquivo.java:28)
at AbreDat.main(AbreDat.java:14)[/quote]
Alguma luz ?