/*** 字符输出流*/
public static voidtest2() {
File file= new File("dest1.txt");
Writer fw= null;try{
fw= newFileWriter(file);
String str= "大家好,我是康纳\r哈哈";//写法 1//char[] charArray = str.toCharArray();//字符串到 字节数组//fw.write(charArray, 0, charArray.length);//写法 2//fw.write(str);//写法3
fw.append("IO is so easy\r ").append("大家好我是康纳郭");
fw.flush();
}catch(Exception e) {
e.printStackTrace();
}finally{try{if (fw != null) {
fw.close();
}
}catch(Exception e2) {
e2.printStackTrace();
}
}
}/*** 字符输入流*/
public static voidtest1() {
File file= new File("test.txt");//字符输入流
Reader fr = null;try{
fr= newFileReader(file);//操作缓冲区
char[] flush = new char[10];//每次接受字符长度
int len = -1;while ((len = fr.read(flush)) != -1) {//字符数组 到字符串
String str = new String(flush, 0, len);
System.out.print(str);
}
}catch(Exception e) {
e.printStackTrace();
}finally{try{//关流
if (fr != null) {
fr.close();
}
}catch(Exception e2) {
e2.printStackTrace();
}
}
}/*** 字符流 文件的拷贝
*
*@paramsrcPath 要复制的文件
*@paramdestPath 复制到该文件中*/
public static voidcopy(String srcPath, String destPath) {//创建源
File src = newFile(srcPath);
File dest= newFile(destPath);//创建字符流
Reader reader = null;
Writer writer= null;try{
reader= newFileReader(src);
writer= newFileWriter(dest);int len = -1;//每次读取的长度
char[] flush = new char[2];//缓冲器//int i = 0;
while ((len = reader.read(flush)) != -1) {//writer.write(i+" ");
writer.write(flush, 0, len);//writer.write("\r\n");
}
writer.flush();//刷新
} catch(Exception e) {
e.printStackTrace();
}finally{//关流
try{if (writer != null) {
writer.close();
}if (reader != null) {
reader.close();
}
}catch(Exception e2) {
e2.printStackTrace();
}
}
}