A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王健宇 中级黑马   /  2012-10-22 19:07  /  1265 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

File f=new File("d:"+File.separator+"text.txt");
                        OutputStream out=new FileOutputStream(f);
                        String s="hello world;";
                        byte b[]=s.getBytes();
                        out.write(b);
                        /*out.close();*/
text文件中有“hello world;”
但是用
File f=new File("d:"+File.separator+"text.txt");
                        Write out=new FileWrite(f);
                        String s="hello world;";
                        out.write(s);
                        /*out.close();*/
在text中就没有这是为什么呢?

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1

查看全部评分

7 个回复

倒序浏览
怎么还有write类型,自己写的吗?
回复 使用道具 举报
这是两段程序
回复 使用道具 举报
这个我测试了,没什么问题,可能你的类型write写错了  应该是writer
回复 使用道具 举报
楼主写错了吧,应该是 FileWriter out = new FileWriter(f);
回复 使用道具 举报

  1.                 File f = new File("d:" + File.separator + "text.txt");
  2.                 FileOutputStream out = new FileOutputStream(f);
  3.                 String s = "hello world;";
  4.                 byte b[] = s.getBytes();
  5.                 out.write(b);
  6.                
  7.                 File f = new File("d:" + File.separator + "text.txt");
  8.                 Writer out = new FileWriter(f);
  9.                 String s = "hello world;";
  10.                 out.write(s);
  11.                 out.close();
复制代码
第一段代码OutputSteam直接对文件进行了写操作。
第二段代码Writer 具备缓冲功能,需要使用flush()方法或者close()方法来迫使文件写入数据

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1

查看全部评分

回复 使用道具 举报
楼主写错了,wtiter写成了write
回复 使用道具 举报
李铁 中级黑马 2012-10-22 20:59:36
8#
File f=new File("d:"+File.separator+"text.txt");
                         OutputStream out=new FileOutputStream(f);
                         String s="hello world;";
                         byte b[]=s.getBytes();
                         out.write(b);
                         /*out.close();*/
text文件中有“hello world;”
原因是 FileOutputStream是字节输出流,不用关闭也会自动刷新流中的数据到文件中。
但是用
File f=new File("d:"+File.separator+"text.txt");
                         Write out=new FileWrite(f);//应该写在Writer
                         String s="hello world;";
                         out.write(s);
                         /*out.close();*/
在text中就没有这是为什么呢?
原因是Writer是写入字符流的抽象类,要么flush(),要么close()后流中的数据才会写到文件中,一般情况这两步都要写上。

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马