package java9_4;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
/**
*
* 字节流
* InputStream OutputStream
* 字符流
* Writer Reader
*
*/
public class java9_4DemoNo1 {
//获取系统的换行符
private static final String LINE_SEPARATOR = System.getProperty(“line.separator”);
public static void main(String[] args) throws Exception {
FilewriteDemo();
FilereaderDemo();
writerreaderDemo();
}
public static void FilewriteDemo() throws Exception {
FileWriter fw = new FileWriter("IO9-47NO1.txt");
fw.write("zhangsan"+LINE_SEPARATOR+"lisi"+LINE_SEPARATOR+"wangwu");
fw.flush();//写入后必须刷新
fw.close();
}
/**
*读取
*/
public static void FilereaderDemo() throws Exception{
FileReader fr = new FileReader("IO9-47NO1.txt");
//按字节进行读取
/*int temp = fr.read();
System.out.println((char)temp);*/
//按数组进行读取
char[] ch = new char[1024];
int len = 0;
while ((len =fr.read(ch))!=-1) {
System.out.println(new String(ch,0,len));
}
fr.close();
}
public static void writerreaderDemo() throws Exception{
FileReader fr = new FileReader("IO9-47NO1.txt");//获取字符流读取对象
BufferedReader br = new BufferedReader(fr);//字符流读取缓冲区对象
FileWriter fw =new FileWriter("IO9-47NO2.txt");//字符流写入对象
BufferedWriter bw = new BufferedWriter(fw);//字符流写入缓冲区对象
/*
//把IO9-47NO1.txt中的内容读取到IO9-47NO2.txt中但是不能读出IO9-47NO1.txt文件中的换行符
String len = null;
while((len = br.readLine())!=null){
bw.write(len);
}*/
//把IO9-47NO1.txt文件中的内容读到IO9-47NO2.txt中能读出IO9-47NO1.txt中的换行符
char[] cbuff = new char[1024];
int len = 0;
while ((len = fr.read(cbuff))!=-1) {
bw.write(cbuff,0,len);
}
br.close();
bw.close();
fr.close();
fw.close();
}
}