EOF未在数据文件中工作以保存值


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

大家好,我正试图通过NETBEANS IDE中的系统输出将数据插入文件中,但问题是当我按CTRL+Z时,它不起作用,程序仍在运行,当我手动停止时,文件中没有保存任何数据。这是我的一段代码。

实际上我不明白当你的逻辑说"输入水果";。我的意思是,你应该读取一个字符串,而不是逐字节读取,在这种情况下,终止符也是一些字符串值,";结束";例如:

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

最新更新