下面这段代码不会输出任何数据,因为在bufferedReader读取文本信息前
FileOutputStream已经将D:\Debug.log覆盖为空文件..根本没有调用write方法..
删除FileOutputStream后就能正常的读取...这是为什么呢
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JOptionPane;public class _Writer
{
/**
 * @param args
 */
public static void main(String[] args)
{
File hosts = new File("D:\\Debug.log");
FileOutputStream os = null;
FileInputStream is = null;
BufferedReader br = null;
BufferedWriter bw = null;
try
{
is = new FileInputStream(hosts);
os = new FileOutputStream(hosts);
br = new BufferedReader(new InputStreamReader(is));
bw = new BufferedWriter(new OutputStreamWriter(os));
String str = null;
while ((str = br.readLine()) != null)
{
System.out.println(str);
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(null, "hosts文件不存在!");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(null, "IO异常!");
} finally
{
try
{
if (os != null)
os.close();
if (is != null)
is.close();
if (br != null)
br.close();
if (bw != null)
bw.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(null, "CLOSE异常!");
}
}
}
}
javaiofileoutputstream奇葩

解决方案 »

  1.   

    new FileOutputStream(hosts); 改成 new FileOutputStream(hosts , true); 表示追加文件
      

  2.   

    就如楼上说的,
     os = new FileOutputStream(hosts);改成
     os = new FileOutputStream(hosts,true);就好了
    至于原因,看源码
      public FileOutputStream(String name) throws FileNotFoundException {
            this(name != null ? new File(name) : null, false);
        }
    继续追踪 public FileOutputStream(File file, boolean append)
            throws FileNotFoundException
        {
            String name = (file != null ? file.getPath() : null);
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                security.checkWrite(name);
            }
            if (name == null) {
                throw new NullPointerException();
            }
            this.fd = new FileDescriptor();
            this.append = append;        fd.incrementAndGetUseCount();
            open(name, append);
        }
    找到open方法 /**
         * Opens a file, with the specified name, for overwriting or appending.
         * @param name name of file to be opened
         * @param append whether the file is to be opened in append mode
         */
        private native void open(String name, boolean append)
            throws FileNotFoundException;
    这里就停了。这里让我们输入参数是重新写还是添加。
    既然输入了,我想这个open方法应该就是根据输入做准备工作吧,比如重新写就清空。。
      

  3.   


    追加文件也读不到信息
    不会吧。。
    我读到了,你先确定下你那个文件是否有内容。。毕竟你清空过好几次了。public static void main(String[] args)
        {
            File hosts = new File("D:\\Debug.log");
            FileOutputStream os = null;
            FileInputStream is = null;
            BufferedReader br = null;
            BufferedWriter bw = null;
            try
            {
                is = new FileInputStream(hosts);
                os = new FileOutputStream(hosts,true);
                br = new BufferedReader(new InputStreamReader(is));
                bw = new BufferedWriter(new OutputStreamWriter(os));
                String str = null;
                while ((str = br.readLine()) != null)
                {
                    System.out.println(str);
                }
            } catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "hosts文件不存在!");
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "IO异常!");
            } finally
            {
                try
                {
                    if (os != null)
                        os.close();
                    if (is != null)
                        is.close();
                    if (br != null)
                        br.close();
                    if (bw != null)
                        bw.close();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "CLOSE异常!");
                }
            }
        }
      

  4.   

    谢谢,我明白了,OUTPUTSTREAM就算不write 也会写入空文件。这个文件只能通过覆盖的方式来修改