EOF não trabalhar em arquivos de dados para salvar o valor

0

Pergunta

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

Olá a todos, estou tentando inserir dados em um arquivo por meio do sistema de saída no NETBEANS IDE, mas o problema é quando eu estou pressionando CTRL+Z não está funcionando, o programa ainda está em execução e que, quando eu parar manualmente não há dados salvos no arquivo. Este é o meu pedaço de código.

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

Melhor resposta

0

Na verdade, eu não entendo qual é a razão para confiar na EOF quando sua lógica diz "Enter frutos". Eu quero dizer que você deve ler uma seqüência de caracteres, não um byte por byte e, neste caso, terminator será também algumas cadeia de valor, "end", por exemplo:

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

Em outros idiomas

Esta página está em outros idiomas

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