1,字节流与字符流写的比较
public static void main2(String[] args) throws IOException {
String fileName="/home/lolashe/javatest"+File.separator+"hello1.txt";File f=new File(fileName);
Writer out =new FileWriter(f);
String str="hello";
// 执行完write(str)之后,关闭之前,我们的文件中并没有看到hello内容
out.write(str);
// 字符流是一个字符一个字符的写,用到了缓冲区。flush()是将缓冲区的内容写如文件中。
//out.flush();
// close方法中也会调用flush()方法
out.close();
}
public static void main(String[] args) throws IOException {
String fileName="/home/lolashe/javatest"+File.separator+"hello3.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f);
String str="你好";
byte[] b=str.getBytes();
// 执行完write(b)之后我们的文件中已经存在“你好”了,因为字节流是一个字节一个字节的写,并没有用到缓冲区
out.write(b);
out.close();
}