FileOutputStream自动覆盖文件的问题

某ZyL 2013-10-30 01:14:26
下面这段代码不会输出任何数据,因为在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异常!");
}
}
}
}
...全文
1340 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
某ZyL 2013-10-30
  • 打赏
  • 举报
回复
谢谢,我明白了,OUTPUTSTREAM就算不write 也会写入空文件。这个文件只能通过覆盖的方式来修改
失落夏天 2013-10-30
  • 打赏
  • 举报
回复
引用 4 楼 a269756 的回复:
[quote=引用 2 楼 is_zhoufeng 的回复:] new FileOutputStream(hosts); 改成 new FileOutputStream(hosts , true); 表示追加文件
引用 3 楼 AA5279AA 的回复:
就如楼上说的, 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方法应该就是根据输入做准备工作吧,比如重新写就清空。。
追加文件也读不到信息[/quote] 不会吧。。 我读到了,你先确定下你那个文件是否有内容。。毕竟你清空过好几次了。

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异常!");
            }
        }
    }
某ZyL 2013-10-30
  • 打赏
  • 举报
回复
引用 2 楼 is_zhoufeng 的回复:
new FileOutputStream(hosts); 改成 new FileOutputStream(hosts , true); 表示追加文件
引用 3 楼 AA5279AA 的回复:
就如楼上说的, 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方法应该就是根据输入做准备工作吧,比如重新写就清空。。
追加文件也读不到信息
失落夏天 2013-10-30
  • 打赏
  • 举报
回复
就如楼上说的, 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方法应该就是根据输入做准备工作吧,比如重新写就清空。。
_jerrytiger 2013-10-30
  • 打赏
  • 举报
回复
new FileOutputStream(hosts); 改成 new FileOutputStream(hosts , true); 表示追加文件
teemai 2013-10-30
  • 打赏
  • 举报
回复
你这是想干嘛啊?干嘛又读又写,还是同一个文件。你的目的是什么?

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧