文件夹基本操作:
File file=new File("D:/aaa/bbb.txt");
删除文件或者文件夹:file.delete
创建文件夹:mkdir(),mkdirs()
创建文件:createNewFile()
遍历文件输入流对象:
字节流:
1)、文件读取:
byte[] b=null;
FileInputStream file;
try{
file=new FileInputStream(path);
int size=file.available();
for(int i=0;i<size;i++){
b[i]=(byte)file.read();
}
file.close;
catch(IOException e){
e.printStackTrace();
}
}
2)、文件写入:
FileOutputStream fos=new FileOutputStram(path);
String str="chudgswuvcsjcdsl";
byte[ ] b=str.getBytes();
fos.write(b);
字符流:
文件读取:
String path=" xxxx ";
// 按字节读
FileReader fr=new FileReader(path);
// 按行读
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while(line !=null){
//继续读,循环,知道line为空退出
line=br.readLine();
}
br.close();
fr.close();
文件写入:
String path=" xxxx ";
FileWriter fw=new FileWriter(path);
BufferedWriter bw=new BufferedWriter(fw);
String str="vchdgsjfls";
bw.write(str);
bw.newLine();
bw.flush();
fw.close();