(1)创建文件夹
File myDir = new File("目录地址");
if (!myDir.exists()) {
myDir.mkdir();
}
(2)删除文件夹
File myDir = new File("");
① 删除空文件夹
myDir.delete();
② 删除文件夹下的所有文件
File[] files = myDir.listFiles();
for (int i = 0 ; i < files.length ; i++){
if (files[i].isDirectory()){
files[i].delete();
}
}
(3)清空文件夹
File myDir = new File("");
(4)创建文件
File myFile = new File("文件地址");
if (!myFile.exists()) {
myFile.createNewFile();
}
FileWriter resultFile = newFileWriter(myFile);
PrintWriter myFileWriter = newPrintWriter(resultFile);
myFileWriter.println(str2);
resultFile.close();
将对象写入文件:
FileOutputStream outstream =newFileOutputStream("library.dat");
ObjectOutputStream out =newObjectOutputStream(outstream);
out.writeObject(me);
out.close();
从文件中读取对象:
ObjectInputStream in =newObjectInputStream(new FileInputStream("library.dat"));
Person who = (Person)in.readObject();
(5)删除文件
File myFile = new File("");
myFile.delete();
(6)读取文件
FileReader fr = newFileReader("");
BufferedReaderReader(fr);
String str = br.readLine();
while (str != null) {
str = br.readLine();
}
br.close();
fr.close();
(7)写入文件
FileWriter fw = new FileWriter(str1);
fw.write(str2);
fw.flush();
fw.close();