📄 fileutil.java
字号:
package jodd.file;
import jodd.util.Util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
/**
* File utilities.
*/
public final class FileUtil {
// ---------------------------------------------------------------- misc utils
/**
* Returns current working folder string.
*
* @return current woking folder
*/
public static String getWorkingDir() {
return System.getProperty("user.dir");
}
/**
* Returns current working folder.
*
* @return File for current woking folder
*/
public static File getWorkingDirFile() {
return new File(System.getProperty("user.dir"));
}
/**
* Returns file size.
*
* @param fileName file name
*
* @return file size
*/
public static long getFileSize(String fileName) {
return new File(fileName).length();
}
/**
* Returns file size.
*
* @param file file
*
* @return file size
*/
public static long getFileSize(File file) {
return file.length();
}
/**
* Checks if two files are poiniting to the same file.
*
* @param file1 first file
* @param file2 second file
*
* @return true if files are pointing to the same file
*/
public static boolean equals(String file1, String file2) {
return equals(new File(file1), new File(file2));
}
/**
* Checks if two files are poiniting to the same file.
*
* @param file1 first file
* @param file2 second file
*
* @return true if files are pointing to the same file
*/
public static boolean equals(File file1, File file2) {
try {
file1 = file1.getCanonicalFile();
file2 = file2.getCanonicalFile();
} catch (IOException e) {
return false;
}
return file1.equals(file2);
}
/**
* Creates all folders.
*
* @param dirs dirs
*
* @return true if creating was successful, false otherwise
*/
public static boolean mkdirs(String dirs) {
return new File(dirs).mkdirs();
}
/**
* Creates all folders.
*
* @param dirs dirs
*
* @return true if creating was successful, false otherwise
*/
public static boolean mkdirs(File dirs) {
return dirs.mkdirs();
}
/**
* Creates a folder.
*
* @param dir dir
*
* @return true if creating was successful, false otherwise
*/
public static boolean mkdir(String dir) {
return new File(dir).mkdir();
}
/**
* Creates a folders.
*
* @param dir dir
*
* @return true if creating was successful, false otherwise
*/
public static boolean mkdir(File dir) {
return dir.mkdir();
}
// ---------------------------------------------------------------- file copy vairants
/**
* Buffer size (32KB) for file manipulation methods.
*/
public static int FILE_BUFFER_SIZE = 32 * 1024;
/**
* Copies and overwrites a file to another file or folder.
*
* @param fileIn input file
* @param fileOut output file
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copy(String fileIn, String fileOut) {
return copy(new File(fileIn), new File(fileOut), FILE_BUFFER_SIZE, true);
}
/**
* Copies a file to another file or folder.
*
* @param fileIn input file
* @param fileOut output file
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copySafe(String fileIn, String fileOut) {
return copy(new File(fileIn), new File(fileOut), FILE_BUFFER_SIZE, false);
}
/**
* Copies and overwrites a file to another file or folder with specified buffer size.
*
* @param fileIn input file
* @param fileOut output file
* @param bufsize size of the buffer used for copying
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copy(String fileIn, String fileOut, int bufsize) {
return copy(new File(fileIn), new File(fileOut), bufsize, true);
}
/**
* Copies a file to another file or folder with specified buffer size.
*
* @param fileIn input file
* @param fileOut output file
* @param bufsize size of the buffer used for copying
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copySafe(String fileIn, String fileOut, int bufsize) {
return copy(new File(fileIn), new File(fileOut), bufsize, false);
}
/**
* Copies and overwrites a file to another file or folder.
*
* @param fileIn input file
* @param fileOut output file
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copy(File fileIn, File fileOut) {
return copy(fileIn, fileOut, FILE_BUFFER_SIZE, true);
}
/**
* Copies a file to another file or folder.
*
* @param fileIn input file
* @param fileOut output file
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copySafe(File fileIn, File fileOut) {
return copy(fileIn, fileOut, FILE_BUFFER_SIZE, false);
}
/**
* Copies and overwrites a file to another file or folder with specified buffer size.
*
* @param fileIn input file
* @param fileOut output file
* @param bufsize size of the buffer used for copying
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copy(File fileIn, File fileOut, int bufsize) {
return copy(fileIn, fileOut, bufsize, true);
}
/**
* Copies a file to another file or folder with specified buffer size.
*
* @param fileIn input file
* @param fileOut output file
* @param bufsize size of the buffer used for copying
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copySafe(File fileIn, File fileOut, int bufsize) {
return copy(fileIn, fileOut, bufsize, false);
}
// ---------------------------------------------------------------- file copy
/**
* Copies a file to another file or folder with specified buffer size and
* overwrite flag.
*
* @param fileIn input file
* @param fileOut output file
* @param bufsize size of the buffer used for copying
* @param overwrite should existing destination be overwritten
*
* @return true if operation was successful, false otherwise
* @see #copy(File, File, int, boolean)
*/
public static boolean copy(String fileIn, String fileOut, int bufsize, boolean overwrite) {
return copy(new File(fileIn), new File(fileOut), bufsize, overwrite);
}
/**
* Copies a file to another file or folder with specified buffer size and
* overwrite flag. If source doesn't exist, operation fails. If source isn't
* file, operation fails. Destination may be a folder, in that case file will
* be copied in destination folder. If overwrite is on and source file points
* to the same file as edstination, operation is successful.
*
* @param fileIn input file
* @param fileOut output file
* @param bufsize size of the buffer used for copying
* @param overwrite should existing destination be overwritten
*
* @return true if operation was successful, false otherwise
*/
public static boolean copy(File fileIn, File fileOut, int bufsize, boolean overwrite) /*throws IOException*/ {
// check if source exists
if (fileIn.exists() == false) {
return false;
}
// check if source is a file
if (fileIn.isFile() == false) {
return false;
}
// if destination is folder, make it to be a file.
if (fileOut.isDirectory() == true) {
fileOut = new File(fileOut.getPath() + File.separator + fileIn.getName());
}
if (overwrite == false) {
if (fileOut.exists() == true) {
return false;
}
} else {
if (fileOut.exists()) { // if overwriting, check if destination is the same file as source
try {
if (fileIn.getCanonicalFile().equals(fileOut.getCanonicalFile()) == true) {
return true;
}
} catch (IOException ioex) {
return false;
}
}
}
return copyFile(fileIn, fileOut, bufsize);
}
// ---------------------------------------------------------------- copy file
/**
* Copies one file to another withut any checkings.
*
* @param fileIn source
* @param fileOut destination
*
* @return true if operation was successful, false otherwise
*/
public static boolean copyFile(String fileIn, String fileOut) {
return copyFile(new File(fileIn), new File(fileOut), FILE_BUFFER_SIZE);
}
/**
* Copies one file to another withut any checkings.
*
* @param fileIn source
* @param fileOut destination
*
* @return true if operation was successful, false otherwise
*/
public static boolean copyFile(File fileIn, File fileOut) {
return copyFile(fileIn, fileOut, FILE_BUFFER_SIZE);
}
/**
* Copies one file to another withut any checkings.
*
* @param fileIn source
* @param fileOut destination
* @param bufsize buffer size
*
* @return true if operation was successful, false otherwise
*/
public static boolean copyFile(String fileIn, String fileOut, int bufsize) {
return copyFile(new File(fileIn), new File(fileOut), bufsize);
}
/**
* Copies one file to another withut any checkings.
*
* @param fileIn source
* @param fileOut destination
* @param bufsize buffer size
*
* @return true if operation was successful, false otherwise
*/
public static boolean copyFile(File fileIn, File fileOut, int bufsize) {
FileInputStream in = null;
FileOutputStream out = null;
boolean result = false;
try {
in = new FileInputStream(fileIn);
out = new FileOutputStream(fileOut);
Util.copyPipe(in, out, bufsize);
result = true;
} catch(IOException ioex) {
} finally {
if (out != null) {
try {
out.close();
} catch(IOException ioex) {
}
}
if (in != null) {
try {
in.close();
} catch(IOException ioex) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -