IO读取和写入
package com.hkd.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class FileManager {
StringBuffer sb = new StringBuffer();
public void readerFile(String path){
try {
Reader r = new FileReader(path);
char[] cs = new char[4096];//创建一个字符缓冲区
int length = r.read(cs);//第一次读取
while(length>0){
sb.append(cs, 0, length);
length=r.read(cs);//再次读取
}
r.close();
System.out.println(sb);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeFile(String content,String path){
try {
Writer w = new FileWriter(path);
//String content=sb.toString();//获取读到的数据给content,其实就是把拿到读到的数据进行写入到指定路径,就是复制。
w.write(content);
w.close();
System.out.println(content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试类:
package com.hkd.io;
public class ManageMain {
/**
* @param args
*/
public static void main(String[] args) {
FileManager fm = new FileManager();
fm.readerFile("e:\\AA\\hello.txt");
//fm.writeFile(path);
fm.writeFile("hello word","e:\\AA\\tt.txt");
}
}
以后为了方便使用,我们可以直接将其做成jar包