package IO;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
//从文档输入到控制台
Test2.testFileReader(“F:\IO_test\test\tt11.txt”);
//将代码的信息输出文档
// Test2.testFileWritr(“F:\IO_test\test\tt8.txt”);
// Test2.copyFile(“F:\IO_test\test\tt1.txt”,“F:\IO_test\test\tt\tt1.txt”);
}
/**
* 文件字符输入流
* @param inPath
*/
public static void testFileReader(String inPath){
try {
FileReader fr = new FileReader(inPath); //创建就文件字符输入流的对象
char [] c = new char[10];
int len = 0 ; //定义一个输入流的读取长度
while ((len = fr.read(c)) !=-1){
System.out.print(new String(c,0,len));
}
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件字符输出流
* @param outPath
*/
public static void testFileWritr(String outPath){
String str = "Real love is always worth waiting for";
try {
FileWriter fw = new FileWriter(outPath);
fw.write(str); //写的内存中
fw.flush(); // 写到硬盘中
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 字符流完成拷贝文件,字符流只适合操作内容是字符文件
* @param inPath
* @param outPath
*/
public static void copyFile (String inPath, String outPath) {
try {
FileReader fr = new FileReader(inPath);
FileWriter fw = new FileWriter(outPath);
char[] c = new char[100]; // 缓冲组
int len = 0;
while ((len = fr.read(c)) != -1){
fw.write(c,0,len);
}
fw.flush();
fw.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}