package cn.piesat.safe.common;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.*;
public class FileUtil {
public static void main(String[] args) throws Exception{
File file = new File(“D:\公司入职培训”);
List
try {
File[] files = file.listFiles();
for (File fileOne : files) {
Map map = new HashMap<>();
Integer isDir = fileOne.isFile() ? 1 : 0;
if (!status && isDir == 1) {
//如果不需要文件则在此进行返回过滤
continue;
}
List> subdirectory = new ArrayList<>();
String path = fileOne.getPath();
map.put("fileName", fileOne.getName());
map.put("path", fileOne.getPath());
map.put("fileTime", fileCreateDate(path));
map.put("isDir", isDir);
if (isDir==1){
Path filePath = fileOne.toPath();
String mimeType = Files.probeContentType(filePath);
System.out.println(mimeType);
map.put("fileType", mimeType);
}
if (fileOne.isDirectory()) {
filedi(fileOne, subdirectory, status);
}
map.put("subdirectory", subdirectory);
result.add(map);
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 获取文件的创建时间
*/
public static String fileCreateDate(String filePath){
Path path= Paths.get(filePath);
BasicFileAttributeView basicview= Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS );
BasicFileAttributes attr;
try {
attr = basicview.readAttributes();
Date createDate = new Date(attr.creationTime().toMillis());
String format = format(createDate, "yyyy-MM-dd HH:mm:ss");
return format;
// return createDate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 格式化日期
* - yyyy-MM-dd HH:mm:ss
*
* @param date 日期
* @param pattern 日期格式
* @return 日期字符串
*/
public static String format(Date date, String pattern) {
SimpleDateFormat sd = new SimpleDateFormat(pattern);
return sd.format(date);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)