io.github.nichetoolkit.rest.helper.FileHelper Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.helper;
import io.github.nichetoolkit.rest.error.often.FileCopyException;
import io.github.nichetoolkit.rest.error.often.FileCreateException;
import io.github.nichetoolkit.rest.util.NameUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Map;
/**
* FileHelper
* The file helper class.
* @author Cyan ([email protected])
* @see lombok.extern.slf4j.Slf4j
* @see java.lang.SuppressWarnings
* @since Jdk1.8
*/
@Slf4j
@SuppressWarnings("SameNameButDifferent")
public class FileHelper {
/**
* createFile
* The create file method.
* @param path {@link java.lang.String} The path parameter is String
type.
* @return {@link java.io.File} The create file return object is File
type.
* @throws FileCreateException {@link io.github.nichetoolkit.rest.error.often.FileCreateException} The file create exception is FileCreateException
type.
* @see java.lang.String
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.FileCreateException
*/
public static File createFile(final String path) throws FileCreateException {
return createFile(new File(path));
}
/**
* createFile
* The create file method.
* @param path {@link java.lang.String} The path parameter is String
type.
* @param name {@link java.lang.String} The name parameter is String
type.
* @return {@link java.io.File} The create file return object is File
type.
* @throws FileCreateException {@link io.github.nichetoolkit.rest.error.often.FileCreateException} The file create exception is FileCreateException
type.
* @see java.lang.String
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.FileCreateException
*/
public static File createFile(final String path, final String name) throws FileCreateException {
String filePath = path.concat(File.separator).concat(name);
return createFile(new File(filePath));
}
/**
* createFile
* The create file method.
* @param path {@link java.lang.String} The path parameter is String
type.
* @param nameMap {@link java.util.Map} The name map parameter is Map
type.
* @return {@link java.io.File} The create file return object is File
type.
* @throws FileCreateException {@link io.github.nichetoolkit.rest.error.often.FileCreateException} The file create exception is FileCreateException
type.
* @see java.lang.String
* @see java.util.Map
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.FileCreateException
*/
public static File createFile(final String path, final Map nameMap) throws FileCreateException {
String filePath = path.concat(File.separator).concat(nameMap.get(NameUtils.NAME)).concat(nameMap.get(NameUtils.EXT));
return createFile(new File(filePath));
}
/**
* createFile
* The create file method.
* @param path {@link java.lang.String} The path parameter is String
type.
* @param name {@link java.lang.String} The name parameter is String
type.
* @param ext {@link java.lang.String} The ext parameter is String
type.
* @return {@link java.io.File} The create file return object is File
type.
* @throws FileCreateException {@link io.github.nichetoolkit.rest.error.often.FileCreateException} The file create exception is FileCreateException
type.
* @see java.lang.String
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.FileCreateException
*/
public static File createFile(final String path, final String name, final String ext) throws FileCreateException {
String filePath = path.concat(File.separator).concat(name).concat(ext);
return createFile(new File(filePath));
}
/**
* createFile
* The create file method.
* @param file {@link java.io.File} The file parameter is File
type.
* @return {@link java.io.File} The create file return object is File
type.
* @throws FileCreateException {@link io.github.nichetoolkit.rest.error.often.FileCreateException} The file create exception is FileCreateException
type.
* @see java.io.File
* @see java.lang.SuppressWarnings
* @see io.github.nichetoolkit.rest.error.often.FileCreateException
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public static File createFile(final File file) throws FileCreateException {
if (file.exists()) {
return file;
}
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException exception) {
throw new FileCreateException(exception.getMessage());
}
return file;
}
/**
* copyFile
* The copy file method.
* @param srcFile {@link java.io.File} The src file parameter is File
type.
* @param targetFile {@link java.io.File} The target file parameter is File
type.
* @throws FileCopyException {@link io.github.nichetoolkit.rest.error.often.FileCopyException} The file copy exception is FileCopyException
type.
* @see java.io.File
* @see io.github.nichetoolkit.rest.error.often.FileCopyException
*/
public static void copyFile(final File srcFile, final File targetFile) throws FileCopyException {
try (
FileInputStream fileInputStream = new FileInputStream(srcFile);
FileChannel input = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
FileChannel output = fileOutputStream.getChannel()) {
output.transferFrom(input, 0, input.size());
} catch (IOException exception) {
throw new FileCopyException(exception.getMessage());
}
}
/**
* copyFile
* The copy file method.
* @param srcPath {@link java.lang.String} The src path parameter is String
type.
* @param targetPath {@link java.lang.String} The target path parameter is String
type.
* @throws FileCopyException {@link io.github.nichetoolkit.rest.error.often.FileCopyException} The file copy exception is FileCopyException
type.
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.often.FileCopyException
*/
public static void copyFile(final String srcPath, final String targetPath) throws FileCopyException {
File srcFile = new File(srcPath);
File targetFile = new File(targetPath);
copyFile(srcFile, targetFile);
}
/**
* deleteFile
* The delete file method.
* @param path {@link java.lang.String} The path parameter is String
type.
* @return boolean The delete file return object is boolean
type.
* @see java.lang.String
*/
public static boolean deleteFile(final String path) {
File file = new File(path);
return deleteFile(file);
}
/**
* deleteFile
* The delete file method.
* @param file {@link java.io.File} The file parameter is File
type.
* @return boolean The delete file return object is boolean
type.
* @see java.io.File
*/
public static boolean deleteFile(final File file) {
if (file.exists() && file.isFile()) {
return file.delete();
} else {
return true;
}
}
/**
* clearFile
* The clear file method.
* @param path {@link java.lang.String} The path parameter is String
type.
* @return boolean The clear file return object is boolean
type.
* @see java.lang.String
*/
public static boolean clearFile(final String path) {
boolean flag = true;
File file = new File(path);
if (!file.exists() || !file.isDirectory()) {
return false;
}
String[] tempList = file.list();
String tempPath;
for (int i = 0; tempList != null && i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
tempPath = path.concat(tempList[i]);
} else {
tempPath = path.concat(File.separator).concat(tempList[i]);
}
File temp = new File(tempPath);
if (temp.isFile()) {
flag = flag && temp.delete();
} else if (temp.isDirectory()) {
String subPath = path.concat(File.separator).concat(tempList[i]);
flag = flag && clearFile(subPath);
}
}
return flag && file.delete();
}
}