创建文件夹
//创建文件夹
public static void OnCreateFolder() {
String path = "E:\temp\t01\t02\t03";
File file = new File(path);
try {
if (!file.exists()) {
// boolean result = file.mkdir();
boolean result = file.mkdirs();
System.out.println("create " + (result?"success":"fail"));
//mkdir 只能创建一层目录, 父目录必须存在
//mkdirs 必需但不存在的父目录也会一同创建出来
}
}
catch (Exception e) {
System.out.println("error mkdir");
e.printStackTrace();
}
}
创建文件
//创建文件
public static void OnCreateFile() {
String path = "E:\temp\file_01.doc";
File file = new File(path);
try {
if (!file.exists()) {
boolean result = file.createNewFile();
System.out.println("create " + (result?"success":"fail"));
//父目录必须存在
}
}
catch (Exception e) {
System.out.println("error createNewFile");
e.printStackTrace();
}
}
删除文件/空文件夹
//删除文件/空文件夹
public static void OnDeleteFile() {
// String path = "E:\temp\file_01.doc";
String path = "E:\temp\t01\t02\";
File file = new File(path);
try {
boolean result = file.delete();
System.out.println("delete " + (result?"success":"fail"));
}
catch (Exception e) {
System.out.println("error delete");
e.printStackTrace();
}
}
文件属性
//文件属性
public static void OnProperty() {
// String path = "E:\temp\t01\t02\t03";
String path = "E:\temp\file_01.doc";
File file = new File(path);
if (file.exists()) {
System.out.println(file.getName() + "的属性如下n文件长度为:" + file.length());
System.out.println(file.isFile() ? "是文件" : "不是文件");
System.out.println(file.isDirectory() ? "是目录" : "不是目录");
System.out.println(file.canRead() ? "可读取" : "不");
System.out.println(file.canWrite() ? "可写入" : "");
System.out.println("最后修改日期为: " + new Date(file.lastModified()));
System.out.println("绝对路径: " + file.getAbsolutePath());
}
}
字符流写入文件
//字符流写入文件
public static void OnFileWriteBuffer() {
String path = "E:\temp\file_01.txt";
File file = new File(path);
String content = "新内容: " + new Date() + " File Writern";
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
if (!file.exists()) {
boolean result = file.createNewFile();
System.out.println("create " + (result?"success":"fail"));
//父目录必须存在
}
boolean append = true; //追加 or 覆盖
fileWriter = new FileWriter(file.getAbsoluteFile(), append);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(content);
bufferedWriter.close();
fileWriter.close();
}
catch (Exception e) {
System.out.println("error file write");
e.printStackTrace();
}
}
字节流写入文件
//字节流写入文件
public static void OnFileWriteStream() {
String path = "E:\temp\file_01.txt";
File file = new File(path);
String content = "新内容: " + new Date() + " File Streamn";
FileOutputStream fileOutputStream = null;
try {
if (!file.exists()) {
boolean result = file.createNewFile();
System.out.println("create " + (result?"success":"fail"));
//父目录必须存在
}
boolean append = true; //追加 or 覆盖
fileOutputStream = new FileOutputStream(file, append);
fileOutputStream.write(content.getBytes(StandardCharsets.UTF_8));
fileOutputStream.close();
}
catch (Exception e) {
System.out.println("error file write");
e.printStackTrace();
}
}
Access写入文件
//Access写入文件
public static void OnFileWriteAccess() {
String path = "E:\temp\file_01.txt";
File file = new File(path);
String content = "新内容: " + new Date() + " Accessn";
RandomAccessFile randomAccessFile = null;
try {
if (!file.exists()) {
boolean result = file.createNewFile();
System.out.println("create " + (result?"success":"fail"));
//父目录必须存在
}
randomAccessFile = new RandomAccessFile(path, "rw"); //r:只读 rw:读写
//清空原来的数据
randomAccessFile.setLength(0);
//将写文件指针移到文件尾
randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.write(content.getBytes(StandardCharsets.UTF_8));
randomAccessFile.close();
}
catch (Exception e) {
System.out.println("error file write");
e.printStackTrace();
}
}
字符流读取文件
//字符流读取文件
public static String OnFileReadBuffer() {
String s = "";
String path = "E:\temp\file_01.txt";
File file = new File(path);
if (!file.exists())
return "";
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(file));
String temp;
while((temp=bufferedReader.readLine()) != null) {
s += temp + "n";
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null)
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("file content:n" + s);
return s;
}
0
0
0
0
工具: 清空文件夹
//工具: 清空文件夹
public static boolean onDeleteDirectory(String path) {
File file = new File(path);
if(!file.exists()) { //判断是否待删除目录是否存在
System.err.println("the directory are not exists");
return false;
}
String[] content = file.list(); //取得当前目录下所有文件和文件夹
for(String name : content) {
File temp = new File(path, name);
if(temp.isDirectory()) { //判断是否是目录
onDeleteDirectory(temp.getAbsolutePath()); //递归调用,删除目录里的内容
temp.delete(); //删除空目录
} else {
if(!temp.delete()) {//直接删除文件
System.err.println("failed to delete " + name);
}
}
}
return true;
}