FileOutputStream用于循环的Writebytes,但没有数据写入?

时间:2012-05-27 01:56:23

标签: java file-io fileoutputstream

列出我的程序片段,如下所示

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

}

运行此程序 - &gt; open deo.txt - &gt;此文件中没有数据

但是当我评论for循环时,只测试下面的片段代码:

   try {
            file.write("AB ".getBytes());
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

现在我可以在文件中看到“AB”字符串。太奇怪了......

任何人都可以帮我一个忙吗?

2 个答案:

答案 0 :(得分:0)

您的代码中没有错误。必须生成包含AB AB ...............

的文件“deo.txt”

答案 1 :(得分:0)

我测试了你的代码。它有效。但对于deo.txt。如果它大约是293k,你可以检查它的大小。如果用Eclipse文本编辑器打开它,它什么都不显示。但您可以使用其他系统编辑器查看它,例如记事本++。

相关问题