IO
File 类用来获取文件或者目录属性
InputStream is=new FileInputStream("路径")
int length=is.read(字节数组)
is.close();
OutputStream os=new FileOutputStream("路径")
os.write(字节数组)
os.close();
Reader r=new FileReader("路径")
int length=r.read(字符数组);
r.close();
Write w=new FileWriter("路径");
w.write(内容);
w.close();
以下是代码示例:
package com.hkd.file;
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 {
public String ReadFile(String path){
StringBuffer sb = new StringBuffer();
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();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
/*
*
*/
public void writeFile(String content,String path){
try {
Writer w = new FileWriter(path);
w.write(content);
w.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.hkd.file;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileManager fm =new FileManager();
fm.ReadFile("d:\\123.txt");
fm.writeFile("我是谁", "D:\\789.txt");
}
}