java 中 文件 形成,读取,上传 及 获取某个文件夹下的所有文件 的静态类
此文件会随着时间不断更新
package com.link.test.util;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FileUtil {
private static boolean before(String path, File inputFile) {
File inputPath = new File(path);
try {
return !inputPath.exists() && inputPath.mkdirs() && !inputFile.exists() && inputFile.createNewFile()
||
!inputFile.exists() && inputFile.createNewFile()
||
inputFile.exists();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static String formation(String path, String name, String suffix, String textarea) {
String fileName = name + "." + suffix;
try {
File inputFile = new File(path, fileName);
if (before(path, inputFile)) {
FileOutputStream fileOutputStream = new FileOutputStream(inputFile);
byte[] contentInBytes = textarea.getBytes();
fileOutputStream.write(contentInBytes);
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
public static String formationByByte(String path, String name, String suffix, byte[] fileByte) {
String fileName = name + "." + suffix;
try {
File inputFile = new File(path, fileName);
if (before(path, inputFile)) {
FileOutputStream fileOutputStream = new FileOutputStream(inputFile);
fileOutputStream.write(fileByte);
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
public static List<String> read(String filePath, String encoding) {
List<String> result = new ArrayList<>();
try {
BufferedReader br = getReader(filePath, encoding);
String s;
while ((s = br.readLine()) != null) {
result.add(s);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static BufferedReader getReader(String filePath, String encoding) throws IOException {
return new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encoding));
}
public static String upload(String path, String name, String suffix, MultipartFile file) {
String newFileName = name + "." + suffix;
try {
File uploadFile = new File(path, newFileName);
if (before(path, uploadFile)) {
file.transferTo(uploadFile);
}
} catch (Exception e) {
e.printStackTrace();
}
return newFileName;
}
public static String uploadByStream(String path, String name, String suffix, MultipartFile file) {
String newFileName = name + "." + suffix;
try {
File uploadFile = new File(path, newFileName);
byte[] bytes = file.getBytes();
if (before(path, uploadFile)) {
FileOutputStream fileOutputStream = new FileOutputStream(uploadFile);
fileOutputStream.write(bytes);
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return newFileName;
}
public static List<String> uploadReadFile(MultipartFile file, String charsetName) {
BufferedReader reader = null;
List<String> data = new ArrayList<>();
try {
Reader read = new InputStreamReader(file.getInputStream(), charsetName);
reader = new BufferedReader(read);
String tmpString;
while ((tmpString = reader.readLine()) != null) {
data.add(tmpString);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
public static List<String> getAllFileName(String path) {
File file = new File(path);
String[] list = file.list();
assert list != null;
return Arrays.asList(list);
}
public static boolean fileMove(String oldFile, String newPath) {
try {
File file = new File(oldFile);
File moveFile = new File(newPath);
FileUtils.moveFileToDirectory(file, moveFile, true);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}