本帖最后由 javaeea 于 2015-9-26 20:33 编辑
package com.heima.gaoshuai.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ReaderAndWrriter {
public static void main(String[] args) throws IOException {
demo01();
demo02();
demo03();
}
public static void demo03() throws IOException{
BufferedReader bfr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("a1.txt"));
//String str ;
String line;
while((line=bfr.readLine())!=null){
bfw.write(line);
bfw.newLine();//跨平台
}
bfw.close();
bfr.close();
}
public static void demo02() throws IOException{
FileReader fr = new FileReader("a.txt");//底层实现是inputstreamreader
FileWriter fw = new FileWriter("c.txt");//底层实现是outputstramwriter
int len ;
//byte[] bytes = new byte[1024*8];
char[] chars = new char[1024*8];//==========>bufferedReader
while((len=fr.read())!=-1){
fw.write(len);
fw.flush();//or close 才 flush
}
fr.close();
fw.close();
}
public static void demo01() throws IOException{
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("c.txt");
int a ;//asicc 读取单个字节/字符是返回的是asicc码表值
while((a=fr.read())!=-1){
fw.write(a);//2k 超过两个字节可以自动刷新 队列 缓冲字节数组 一个字节读写会乱码
}
fr.close();
fw.close();
}
}
|
|