黑马程序员技术交流社区
标题:
字节流和字符流的问题
[打印本页]
作者:
王健宇
时间:
2012-10-22 19:07
标题:
字节流和字符流的问题
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中就没有这是为什么呢?
作者:
章闽
时间:
2012-10-22 19:12
怎么还有write类型,自己写的吗?
作者:
王健宇
时间:
2012-10-22 19:14
这是两段程序
作者:
于连林
时间:
2012-10-22 19:26
这个我测试了,没什么问题,可能你的类型write写错了 应该是writer
作者:
吴兵
时间:
2012-10-22 19:45
楼主写错了吧,应该是 FileWriter out = new FileWriter(f);
作者:
qhasilver
时间:
2012-10-22 19:52
File f = new File("d:" + File.separator + "text.txt");
FileOutputStream out = new FileOutputStream(f);
String s = "hello world;";
byte b[] = s.getBytes();
out.write(b);
File f = new File("d:" + File.separator + "text.txt");
Writer out = new FileWriter(f);
String s = "hello world;";
out.write(s);
out.close();
复制代码
第一段代码OutputSteam直接对文件进行了写操作。
第二段代码Writer 具备缓冲功能,需要使用flush()方法或者close()方法来迫使文件写入数据
作者:
刘学宾
时间:
2012-10-22 20:07
楼主写错了,wtiter写成了write
作者:
李铁
时间:
2012-10-22 20:59
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()后流中的数据才会写到文件中,一般情况这两步都要写上。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2