工具类:
import com.alibaba.druid.util.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.poi.util.IOUtils;
import javax.swing.JFileChooser;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* 文件操作工具类
* @author zql
* @createTime 2020-12-01 21:57:28
* @version 1.2
* @modifyLog
* 1.1 优化代码
*
* 1.2 一、新增将输入流转化为字节数组方法
* 二、新增输入流转化Base64编码方法
*
*/
public class FileUtil {
/**
* 获取文件列表
* @author zql
* @createTime 2020-12-01 22:00:30
*
* @param fileList 保存File对象的集合
* @param strPath 路径
* @param bl 是否获取全部文件,true获取全部,false只获取路径下的文件,不包括路径下子文件夹的文件
* @return
*/
public List<File> getFileList(List<File> fileList, String strPath, boolean bl) {
File dir = new File(strPath);
// 该文件目录下文件全部放入数组
File[] files = dir.listFiles();
// 因为System Volume Information文件夹是无法访问的,所以需要判断不为空
if (Objects.nonNull(files)) {
for (int i = 0; i < files.length; i++) {
// 判断是文件还是文件夹,如果是,是否获取全部文件
if (files[i].isDirectory() && bl) {
// 获取文件绝对路径
this.getFileList(fileList, files[i].getAbsolutePath(), bl);
// 判断是文件还是文件夹,如果是文件夹,不添加到文件列表
} else if (files[i].isDirectory()) {
continue;
} else {
fileList.add(files[i]);
}
}
}
return fileList;
}
/**
* 获取文件夹列表
* @author zql
* @createTime 2020-12-01 22:01:57
*
* @param folderList 保存File对象的集合
* @param strPath 路径
* @return
*/
public List<File> getFolderList(List<File> folderList, String strPath) {
File dir = new File(strPath);
// 该文件目录下文件全部放入数组
File[] files = dir.listFiles();
// 因为System Volume Information文件夹是无法访问的,所以需要判断不为空
if (Objects.nonNull(files)) {
for (int i = 0, l = files.length; i < l; i++) {
// 如果是文件夹,就添加入列表
if (files[i].isDirectory()) {
// 递归放在folderList之前,这样是为了输出时能先输出最深层的文件夹
this.getFolderList(folderList, files[i].getPath());
folderList.add(files[i]);
}
}
}
return folderList;
}
/**
* 获取路径下的文件名列表,文件名包括文件名扩展名
* @author zql
* @createTime 2020-12-01 22:04:45
*
* @param strPath 路径
* @param bl 是否获取全部文件名列表,true获取全部,包括子文件夹下的文件,false只获取路径下的文件,不包括文件夹
* @return 返回字符串类型的文件名数组
*/
public String[] getFileName(String strPath,boolean bl) {
List<File> files = new ArrayList<File>();
List<File> fileList = this.getFileList(files, strPath, bl);
String[] fileName = new String[fileList.size()];
for (int i = 0; i < fileList.size(); i++) {
fileName[i] = fileList.get(i).getName();
}
return fileName;
}
/**
* 获取路径下的文件的全路径,包括文件名扩展名
* @author zql
* @createTime 2020-12-01 22:05:07
*
* @param strPath 路径
* @param bl 是否获取全部文件全路径,true获取全部,包括子文件夹下的文件,false只获取路径下的文件,不包括文件夹
* @return 返回字符串类型的文件名全路径数组
*/
public String[] getFileAllPath(String strPath, boolean bl) {
List<File> files = new ArrayList<File>();
List<File> fileList = this.getFileList(files, strPath, bl);
String[] fileName = new String[fileList.size()];
for (int i = 0; i < fileList.size(); i++) {
fileName[i] = fileList.get(i).getAbsolutePath();
}
return fileName;
}
/**
* 创建目录
* @author zql
* @createTime 2020-12-01 22:05:57
*
* @param destDirName 目标目录名
* @return 目录创建成功返回true,否则返回false
*/
public boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
return false;
}
if (!destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
// 创建单个目录
if (dir.mkdirs()) {
return true;
} else {
return false;
}
}
/**
* 删除文件
* @author zql
* @createTime 2020-12-01 22:06:15
*
* @param filePathAndName 文件路径及名称 如c:/fpn.txt
*/
public void delFile(String filePathAndName) {
try {
Files.deleteIfExists(new File(filePathAndName).toPath());
} catch (Exception e) {
System.out.println("There was an error deleting the file!");
e.printStackTrace();
}
}
/**
* 文件复制方法
*
* @author zql
* @createTime 2021-03-19 10:48:01
*
* @param source 源文件
* @param dest 新文件
* @return
* @throws
*/
public void copyFileUsing(File source, File dest) {
try {
Files.deleteIfExists(dest.toPath());
Files.copy(source.toPath(), dest.toPath());
} catch (Exception e) {
System.out.println("There was an error deleting the file!");
e.printStackTrace();
}
}
/**
* 读取到字节数组0
* @author zql
* @createTime 2020-12-01 22:06:30
*
* @param filePath 路径
* @return
* @throws IOException
*/
@SuppressWarnings("resource")
public byte[] readToByteArray0(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
// file too big 文件太大
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != buffer.length) {
// Could not completely read file 无法完全读取文件
throw new IOException("Could not completely read file " + file.getName());
}
fi.close();
return buffer;
}
/**
* 读取到字节数组1
* @author zql
* @createTime 2020-12-01 22:06:50
*
* @param filePath
* @return
* @throws IOException
*/
public byte[] readToByteArray1(String filePath) throws IOException {
File f = new File(filePath);
if (!f.exists()) {
throw new FileNotFoundException(filePath);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(f));
int bufSize = 1024;
byte[] buffer = new byte[bufSize];
int len = 0;
while (-1 != (len = in.read(buffer, 0, bufSize))) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
}
/**
* 读取到字节数组2
* @author zql
* @createTime 2020-12-01 22:07:03
*
* @param filePath
* @return
* @throws IOException
*/
public byte[] readToByteArray2(String filePath) throws IOException {
File f = new File(filePath);
if (!f.exists()) {
throw new FileNotFoundException(filePath);
}
FileChannel channel = null;
FileInputStream fs = null;
try {
fs = new FileInputStream(f);
channel = fs.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
while ((channel.read(byteBuffer)) > 0) {
// do nothing
// System.out.println("reading");
}
return byteBuffer.array();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取到字节数组3
* <pre>
* Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
* </pre>
* @author zql
* @createTime 2020-12-01 22:07:15
*
* @param filePath
* @return
* @throws IOException
*/
public byte[] readToByteArray3(String filePath) throws IOException {
FileChannel fc = null;
RandomAccessFile rf = null;
try {
rf = new RandomAccessFile(filePath, "r");
fc = rf.getChannel();
MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load();
// System.out.println(byteBuffer.isLoaded());
byte[] result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
// System.out.println("remain");
byteBuffer.get(result, 0, byteBuffer.remaining());
}
return result;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
rf.close();
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取文件内容
* @author zql
* @createTime 2020-12-01 22:07:43
*
* @param filePath 文件全路径
* @param methodType 读取字节的方法
* <pre>
* 有四个方法,methodType范围0-3
* 其中3 可以在处理大文件时,提升性能
* </pre>
* @return
*/
public String getFileContent(String filePath, int methodType) {
String str = null;
boolean past = methodType < 0 || methodType > 3;
if (past) {
methodType = 0;
}
byte[] b;
try {
switch (methodType) {
case 0:
b = this.readToByteArray0(filePath);
break;
case 1:
b = this.readToByteArray1(filePath);
break;
case 2:
b = this.readToByteArray2(filePath);
break;
case 3:
b = this.readToByteArray3(filePath);
break;
default:
b = this.readToByteArray0(filePath);
}
str = new String(b, "utf-8");
} catch (IOException e) {
System.out.println("Error reading process!");
e.printStackTrace();
}
return str;
}
/**
* 读取文件内容
* @author zql
* @createTime 2020-12-01 22:10:21
*
* @param filePath
* @return
*/
public String getFileContent(String filePath) {
return this.getFileContent(filePath, 3);
}
/**
* 获取文件的md5
* @author zql
* @createTime 2020-12-01 22:10:47
*
* @param file
* @return 文件的md5
*/
public String getFileMd5(File file) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
return DigestUtils.md5Hex(IOUtils.toByteArray(fileInputStream));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 获取文件的md5
* @author zql
* @createTime 2020-12-01 22:11:02
*
* @param filePath 文件路径,包括文件名及扩展名
* @return 文件的md5
*/
public String getFileMd5(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
System.out.println(filePath + " is not exists!");
return null;
}
return this.getFileMd5(file);
}
/**
* 字符串写入到文件
* @author zql
* @createTime 2020-12-01 22:11:19
*
* @param content 字符串内容
* @param filePath 路径
* @param append true是往文件后追加,false是覆盖
*/
public void writeContentToFile(String content, String filePath, boolean append) {
File file = new File(filePath);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file, append);
// 注意需要转换对应的字符集
out.write(content.getBytes("utf-8"));
out.flush();
out.close();
} catch (IOException e) {
System.out.println("Content writing failed!");
e.printStackTrace();
}
}
/**
* 字符串写入到文件,此写入为覆盖的方式
* @author zql
* @createTime 2020-12-01 22:11:19
*
* @param content 字符串内容
* @param filePath 路径
*/
public void writeContentToFile(String content, String filePath) {
this.writeContentToFile(content, filePath, false);
}
/**
* 获取文件最后修改时间
* @author zql
* @createTime 2020-12-01 22:13:00
*
* @param filePath
* @return
*/
public String getFileModifiedTime(String filePath) {
File file = new File(filePath);
return this.getFileModifiedTime(file);
}
/**
* 获取文件最后修改时间
* @author zql
* @createTime 2020-12-01 22:13:11
*
* @param file
* @return
*/
public String getFileModifiedTime(File file) {
long time = file.lastModified();
String strTime = DateUtil.getDateString(String.valueOf(time), 25);
return strTime;
}
/**
* 获取文件后缀名
* @author zql
* @createTime 2020-12-01 22:14:08
*
* @param str 文件全名或者文件路径,文件路径请包括文件全名
* @return
*/
public String getFileSuffix(String str) {
return str.substring(str.lastIndexOf(".") + 1);
}
/**
* 获取文件后缀名
* @author zql
* @createTime 2020-12-01 22:14:20
*
* @param file
* @return
*/
public String getFileSuffix(File file) {
String str = file.getAbsolutePath();
return this.getFileSuffix(str);
}
/**
* 获取文件的创建时间
* @author zql
* @createTime 2020-12-01 22:14:31
*
* @param filePath
* @return
*/
public String getFileCreateTime(String filePath) {
String startTime = null;
File file = new File(filePath);
String suffix = this.getFileSuffix(filePath);
try {
Process p = Runtime.getRuntime().exec("cmd /C dir " + file.getAbsolutePath() + "/tc");
InputStream input = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String line;
while (Objects.nonNull(line = br.readLine())) {
if (line.endsWith(suffix)) {
startTime = line.substring(0, 17);
startTime = startTime.replace("/", "").replace(":", "").replace(" ", "");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return startTime;
}
/**
* 获取文件的创建时间
* @author zql
* @createTime 2020-12-01 22:16:42
*
* @param file
* @return
*/
public String getFileCreateTime(File file) {
return this.getFileCreateTime(file.getAbsolutePath());
}
/**
* 获取文件的最后访问时间,注:jdk环境需要在1.7或之上
* @author zql
* @createTime 2020-12-01 22:18:19
*
* @param filePath 文件路径
* @return
*/
public String getFileAccessTime(String filePath) {
String time = null;
Path p = Paths.get(filePath);
try {
// 获取文件的属性
BasicFileAttributes att = Files.readAttributes(p, BasicFileAttributes.class);
// 创建时间
/// time = att.creationTime().toMillis() + "";
// 修改时间
/// time = att.lastModifiedTime().toMillis() + "";
// 访问时间
time = att.lastAccessTime().toMillis() + "";
} catch (IOException e) {
e.printStackTrace();
}
return time;
}
/**
* 将InputStream写入本地文件
* @author zql
* @createTime 2021-01-02 11:53:07
*
* @param outPath 输出路径
* @param input 输入流
* @return
* @throws IOException
*/
public void writeToLocal(String outPath, InputStream input) throws IOException {
int index;
byte[] bytes = new byte[1024];
FileOutputStream fos = new FileOutputStream(outPath);
while ((index = input.read(bytes)) != -1) {
fos.write(bytes, 0, index);
fos.flush();
}
input.close();
fos.close();
}
/**
* 输入流转化Base64编码
*
* @author zql
* @createTime 2021-03-30 09:21:47
*
* @param in
* @return
* @throws
*/
public String getFileBase64(InputStream in) throws Exception {
// 将输入流转化为字节数组字符串,并对其进行Base64编码处理
byte[] data = getByte(in);
return Base64.byteArrayToBase64(data);
}
/**
* 将输入流转化为字节数组
*
* @author zql
* @createTime 2021-03-30 09:20:46
*
* @param in
* @return
* @throws
*/
public byte[] getByte(InputStream in) throws Exception {
// 将输入流转化为字节数组
byte[] data = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int bufSize = 1024;
byte[] buffer = new byte[bufSize];
int len = 0;
while (-1 != (len = in.read(buffer, 0, bufSize))) {
bos.write(buffer, 0, len);
}
data = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Objects.nonNull(in)) {
try {
in.close();
} catch (IOException e) {
throw new Exception("输入流关闭异常");
}
}
}
return data;
}
/**
* 弹出jdk自带资源管理窗口,用于选择路径
* @author zql
* @createTime 2020-12-01 22:18:36
*
* @return 选择的路径
*/
public String getSelectPath() {
String selectPath = null;
JFileChooser chooser = new JFileChooser();
// 设置只能选择目录
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectPath = chooser.getSelectedFile().getPath();
}
return selectPath;
}
}
测试类:
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 文件操作工具junit4测试类
* @author zql
* @createTime 2020-12-01 21:57:28
* @version 1.1
* @modifyLog 1.1 优化代码
*/
public class FileUtilTest {
FileUtil fileUtil = new FileUtil();
private String path = "E:/test";
@Test
public void getFileList() {
System.out.println("开始获取全部文件...");
List<File> fileList1 = new ArrayList<File>();
fileList1 = fileUtil.getFileList(fileList1, path, true);
for (File file : fileList1) {
System.out.println(file.getPath());
}
System.out.println("开始获取子文件...");
List<File> fileList2 = new ArrayList<File>();
fileList2 = fileUtil.getFileList(fileList2, path, false);
for (File file : fileList2) {
System.out.println(file.getPath());
}
}
@Test
public void getFolderList() {
List<File> folderList = new ArrayList<File>();
folderList = fileUtil.getFolderList(folderList, path);
for (File file : folderList){
System.out.println(file.getPath());
}
}
@Test
public void getFileName() {
System.out.println("开始获取全部文件名...");
String[] fileNames1 = fileUtil.getFileName(path, true);
for (String fileName : fileNames1) {
System.out.println(fileName);
}
System.out.println("开始获取子文件名...");
String[] fileNames2 = fileUtil.getFileName(path, false);
for (String fileName : fileNames2) {
System.out.println(fileName);
}
}
@Test
public void getFileAllPath() {
System.out.println("开始获取路径下的全部文件的全路径...");
String[] filePaths1 = fileUtil.getFileAllPath(path, true);
for (String filePath : filePaths1) {
System.out.println(filePath);
}
System.out.println("开始获取路径下的子文件的全路径...");
String[] filePaths2 = fileUtil.getFileAllPath(path, false);
for (String filePath : filePaths2) {
System.out.println(filePath);
}
}
@Test
public void createDir() {
// 路径存在,结果为false
boolean bl1 = fileUtil.createDir(path);
System.out.println(bl1);
// 路径不存在,结果为true
boolean bl2 = fileUtil.createDir(path + "/create");
System.out.println(bl2);
}
@Test
public void delFile() {
// 删除path下的test.txt文件
fileUtil.delFile(path + "/test.txt");
}
@Test
public void readToByteArray0() throws IOException {
byte[] arr = fileUtil.readToByteArray0(path + "/test.txt");
String str = new String(arr);
System.out.println(str);
}
@Test
public void readToByteArray1() throws IOException {
byte[] arr = fileUtil.readToByteArray1(path + "/test.txt");
String str = new String(arr);
System.out.println(str);
}
@Test
public void readToByteArray2() throws IOException {
byte[] arr = fileUtil.readToByteArray2(path + "/test.txt");
String str = new String(arr);
System.out.println(str);
}
@Test
public void readToByteArray3() throws IOException {
byte[] arr = fileUtil.readToByteArray3(path + "/test.txt");
String str = new String(arr);
System.out.println(str);
}
@Test
public void getFileContent() {
String str1 = fileUtil.getFileContent(path + "/test.txt", 0);
System.out.println(str1);
String str2 = fileUtil.getFileContent(path + "/test.txt");
System.out.println(str2);
}
@Test
public void getFileMd5() {
String filePath = path + "/test.txt";
File file = new File(filePath);
String md51 = fileUtil.getFileMd5(filePath);
String md52 = fileUtil.getFileMd5(file);
System.out.println(md51);
System.out.println(md52);
System.out.println(md51.equals(md52));
}
@Test
public void writeContentToFile() {
String filePath1 = path + "/test1.txt";
String filePath2 = path + "/test2.txt";
fileUtil.writeContentToFile("测试覆盖写入", filePath1, false);
fileUtil.writeContentToFile("测试覆盖写入", filePath2);
fileUtil.writeContentToFile("测试追加写入", filePath1, true);
}
@Test
public void getFileModifiedTime() {
String filePath = path + "/test.txt";
File file = new File(filePath);
String time1 = fileUtil.getFileModifiedTime(filePath);
String time2 = fileUtil.getFileModifiedTime(file);
System.out.println(time1);
System.out.println(time2);
}
@Test
public void getFileSuffix() {
String filePath = path + "/test.txt";
File file = new File(filePath);
String suffix1 = fileUtil.getFileSuffix(filePath);
String suffix2 = fileUtil.getFileSuffix(file);
System.out.println(suffix1);
System.out.println(suffix2);
}
@Test
public void getFileCreateTime() {
String filePath = path + "/test.doc";
String time1 = fileUtil.getFileCreateTime(filePath);
File file = new File(path + "/test.txt");
String time2 = fileUtil.getFileCreateTime(file);
System.out.println(time1);
System.out.println(time2);
}
@Test
public void getFileAccessTime() {
String filePath = path + "/test.doc";
String time = fileUtil.getFileCreateTime(filePath);
System.out.println(time);
}
@Test
public void getSelectPath() {
String path = fileUtil.getSelectPath();
System.out.println(path);
}
}