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时,它不工作,程序仍在运行,当我手动停止它时,文件中没有保存任何数据。这是我的代码。
1条答案
按热度按时间ki0zmccv1#
实际上我不明白当你的逻辑说“输入水果”时,依赖EOF的原因是什么。我的意思是你应该读一个字符串,而不是一个字节一个字节的,在这种情况下,终止符也将是一些字符串值,例如“end”: