标签:
字符流:读写文件时,以字符为基础
字节输入流 :reader抽象类 常用子类:FileReader
用法:int read(char 【】c,int off,int len)
字节输出流:writer抽象类 常用子类:FileWriter
用法:void writer(char 【】 c ,int off,int len)
demo:
package test;
import java.io.*;
public class TestChar {
public static void main(String args []) {
FileReader fileReader = null;
FileWriter fileWriter = null;
char c [] = new char[100];
try {
fileReader = new FileReader("/Users/ningyu/Desktop/from.txt");
fileWriter = new FileWriter("/Users/ningyu/Desktop/to.txt");
while(true){
int temp = fileReader.read(c, 0, c.length);
if (temp == -1) {
break;
}
fileWriter.write(c, 0, c.length);
}
// System.out.println(temp);
for(int i = 0;i < c.length; i++){
System.out.println(c[i]);
}
} catch (Exception e) {
System.out.println(e);
}finally {
try {
fileReader.close();
fileWriter.close();
} catch (Exception e2) {
System.out.println(e2);
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/ningxiaoge/p/5286388.html