EOF non funziona in file di dati per memorizzare il valore

0

Domanda

    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();
    }

Ciao a tutti, sto cercando di inserire i dati in un file attraverso il sistema di uscita in NETBEANS IDE, ma il problema è quando sto premendo CTRL+Z non funziona, il programma è ancora in esecuzione e quando ho interrotto manualmente non ci sono dati salvati nel file. Questo è il mio pezzo di codice.

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

Migliore risposta

0

In realtà non capisco qual è il motivo per contare su EOF quando la logica dice "Inserire frutti". Voglio dire, si dovrebbe leggere una stringa, non un byte per byte e in questo caso terminator sarà anche qualche valore stringa "fine", per esempio:

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

In altre lingue

Questa pagina è in altre lingue

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