引用:http://www.iteye.com/topic/175121
IO分两种流
字节流 InputStream OutputStream
字符流 Reader Writer
他们都是抽象类
具体实现
字节流 FileInputStream FileOutputStream
字符流 FileReader FileWriter
字节流转换成字符流可以用 InputSteamReader OutputStreamWriter
转换成BufferdReader BufferedWriter 他们具有缓冲区
例如:读取文件 从字节流输入到字符流输入
定义一个字节流:
FileInputStream fileInputStream = new FileInputStream("d:/text.txt"); // 定义一个指向D:/TEXT.TXT 的字节流
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//字节流转换成InputStreamReader
BufferedReader bufferedReader = new BufferedReader(inputSteamReader);
//InputStreamReader 转换成带缓存的bufferedReader
可以把读出来的内容赋值给字符
String ss = new String();
String s;
while((s = bufferedReader.readLine())!=null){
ss += s;
}
例如:写入文件 从字节流输出到字符流输出
FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt"); //定义一个
指向D:/TEXT.TXT文件
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(s);
bufferedWriter.close();
outputStreamWriter.close();
fileOutputStream.close();