java FileOutStream为循环写入字节,但不写入数据?
下面列出我的程序片段
public class InStream {
static FileOutputStream file=null;
static {
try {
file = new FileOutputStream("deo.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
//when i try to replace below infinite loop,
//it is also not able to output my String
//while(ture)
or
//for(;;)
for(int i=0;i<100000;i++){
file.write("AB ".getBytes());
}
//file.flush();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行此程序->;打开deo。txt->;此文件中没有数据
但当我评论for循环时,只需在代码片段下面进行测试:
try {
file.write("AB ".getBytes());
file.close();
} catch (Exception e) {
e.printStackTrace();
}
现在我可以看到文件中的“AB”字符串。真奇怪
谁能帮我个忙吗
# 1 楼答案
你的代码中没有错误。必须生成包含AB的文件“deo.txt”
# 2 楼答案
我测试了你的代码。而且很有效。但对于deo来说。txt。如果大约293k,你可以查看它的大小。如果使用Eclipse文本编辑器打开它,它将不显示任何内容。但您可以使用其他系统编辑器(如notepad++)查看它