vitolito 4 de mai. de 2010
Bom … fiz o seguinte codigo :
import < a href = "http://java.io" > java . io </ a >.* ;
public class EntradaTexto {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) {
// File f = new File("c:\\arquivos\\log.txt");
String linha = "0" ;
try {
while ( ! linha . isEmpty ()) {
InputStreamReader is = new InputStreamReader ( System . in );
BufferedReader in = new BufferedReader ( is );
System . out . println ( "Digite:" );
linha = in . readLine ();
FileWriter fw = new FileWriter ( "c:\\arquivos\\log.txt" );
fw . write ( linha );
}
} catch ( Exception e ){
}
}
}
A questao é que se der o fw.close; e linha.close dentro do while, o while só funciona uma vez, alguma sugestão???
ViniGodoy 4 de mai. de 2010
Se quiser um código equivalente, em java 5:
package teste ;
import java.io.File ;
import java.io.FileNotFoundException ;
import java.io.PrintWriter ;
import java.util.Scanner ;
public class Main {
static final String PATH = "log.txt" ;
public static void main ( String [] args ) throws FileNotFoundException {
Scanner in = new Scanner ( System . in );
String linha ;
PrintWriter out = new PrintWriter ( PATH );
do {
System . out . println ( "Digite: " );
linha = in . nextLine (). trim ();
if ( ! linha . isEmpty ()) {
out . println ( linha );
}
} while ( ! linha . isEmpty ());
out . flush ();
out . close ();
in . close ();
lerArquivo ();
}
private static void lerArquivo () throws FileNotFoundException {
Scanner in = new Scanner ( new File ( PATH ));
while ( in . hasNextLine ()) {
System . out . printf ( "resultado...: %s%n" , in . nextLine ());
}
in . close ();
}
}