<details>
<summary><mark><font color=darkred>点击查看详细内容</font></mark>
package com.io.file.demo;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
</summary>
</details>
public class FileDemo {
public static void main(String[] args) {
File file = new File("E:/vip/a/b/c/d/e/f/ff.txt");
System.out.println("文件名称:"+file.getName());
String parentPath = file.getParent();
System.out.println("文件所在目录:"+parentPath);
File saveDirectory = new File(parentPath);
if (!saveDirectory.exists()) {
saveDirectory.mkdirs();
}
try {
boolean flag = file.createNewFile();
System.out.println(flag ? "成功" : "失败");
} catch (IOException e) {
e.printStackTrace();
}
File[] files = saveDirectory.listFiles();
System.out.println(Arrays.toString(files));
System.out.println("文件的大小:"+file.length());
System.out.println("文件的绝对路径:"+file.getAbsolutePath());
try {
System.out.println("文件的绝对路径:"+file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("获取磁盘可用大小:"+file.getTotalSpace()/(1024*1024*1024)+"G");
System.out.println("获取磁盘剩余空间:"+file.getFreeSpace()/(1024*1024*1024)+"G");
System.out.println("最后的编辑日期:"+new Date(file.lastModified()));
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.isHidden());
System.out.println("-----------------------------------------");
System.out.println("-------------------------------");
File f1 = new File("E:/vip/a/b/c/d/e/f/f.txt");
File f2 = new File("E:/vip/a/b/c/d/e/f/ff.txt");
System.out.println(f1.compareTo(f2));
System.out.println(f1.equals(f2));
System.out.println(file.isFile());
System.out.println(saveDirectory.isDirectory());
System.out.println(saveDirectory.isFile());
}
}
文件搬家
package com.stream.demo.home;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MoveFile {
public static void main(String[] args) {
String srcPath = "C:\\eclipse4.7";
String destPath = "D:\\base";
moveFile(srcPath, destPath);
}
private static void moveFile(String srcPath, String destPath) {
File srcFile = new File(srcPath);
File[] files = srcFile.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
String targetPath = file.getAbsolutePath().replace(srcPath, destPath);
System.out.println(targetPath);
if (file.isDirectory()) {
createDirectory(targetPath);
moveFile(file.getAbsolutePath(), targetPath);
} else {
copyFile(file.getAbsolutePath(), targetPath);
}
file.delete();
}
}
srcFile.delete();
}
private static void createDirectory(String targetPath) {
File dir = new File(targetPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
private static void copyFile(String srcPath, String destPath) {
File srcFile = new File(srcPath);
try (
BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(destPath)));
){
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
srcFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}