EF no funciona en archivos de datos para guardar el valor

0

Pregunta

    public static void main(String[] args) throws IOException {
    InputStream istream;        
    int c;
    final int EOF = -1;
    istream = System.in; 
    FileWriter outFile =  new FileWriter("C:/Users/boamb/Documents/NetBeansProjects/DSA_BSE20BFT/src/week7/Data.txt",true);
    BufferedWriter bWriter = new BufferedWriter(outFile);
    System.out.println("Enter fruits to store in data File – Press Ctrl+Z to end ");    
    while ((c = istream.read()) != EOF)
    bWriter.write(c);
    bWriter.close();
    }

Hola a todos, estoy tratando de insertar datos en un archivo a través de la salida del sistema en el IDE NETBEANS, pero el problema es cuando estoy presionando CTRL+Z no está funcionando, el programa se sigue ejecutando y cuando me pare manualmente no hay datos guardados en el archivo. Este es mi trozo de código.

data-files data-structures java netbeans
2021-11-24 06:11:15
1

Mejor respuesta

0

Realmente no entiendo cuál es la razón para confiar en EF cuando la lógica dice "Introduzca las frutas". Me refiero a que usted debe leer una cadena de caracteres, no un byte por byte y en este caso terminator será también algunos cadena de valor, "fin", por ejemplo:

public static void main( String[] args ) throws IOException{
    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
    FileWriter outFile = new FileWriter( "C:/Users/boamb/Documents/NetBeansProjects/DSA_BSE20BFT/src/week7/Data.txt", true );
    try ( BufferedWriter bWriter = new BufferedWriter( outFile ); ){
        String line;
        while( true ){
            System.out.println( "Enter fruits to store in data File – Enter 'end' to end " );
            line = br.readLine();
            if( "end".equals( line ) ){
                break;
            }
            bWriter.write( line );
            bWriter.newLine();
        }
        bWriter.flush();
    }
}
2021-12-01 09:38:51

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................