📄 fileutil.java
字号:
}
}
return result;
}
// ---------------------------------------------------------------- file move variants
public static boolean move(String fileNameIn, String fileNameOut) {
return move(new File(fileNameIn), new File(fileNameOut), true);
}
public static boolean moveSafe(String fileNameIn, String fileNameOut) {
return move(new File(fileNameIn), new File(fileNameOut), false);
}
public static boolean move(File fileIn, File fileOut) {
return move(fileIn, fileOut, true);
}
public static boolean moveSafe(File fileIn, File fileOut) {
return move(fileIn, fileOut, false);
}
// ---------------------------------------------------------------- file move
/**
* Moves one file to another file or folder with overwrite flag.
*
* @param fileNameIn
* @param fileNameOut
* @param overwrite overwrite flag
*
* @return true if successful, false otherwise
* @see #move(File, File, boolean)
*/
public static boolean move(String fileNameIn, String fileNameOut, boolean overwrite) {
return move(new File(fileNameIn), new File(fileNameOut), overwrite);
}
/**
* Moves one file to another file or folder with overwrite flag. If source
* doesn't exist, oepration fails. If source is not file, oeration fails. If
* destionation is folder, file will be moved to a file with the same name in
* that folder.
*
* @param fileIn source
* @param fileOut destination
* @param overwrite overwrite flag
*
* @return true if successful, false otherwise
*/
public static boolean move(File fileIn, File fileOut, boolean overwrite) {
// 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;
} else {
fileOut.delete(); // delete destination
}
} catch (IOException ioex) {
return false;
}
}
}
return fileIn.renameTo(fileOut);
}
// ---------------------------------------------------------------- move file
/**
* Moves (renames) a file without any check.
*
* @param src source file
* @param dest destination file
*
* @return true if sucess, false otherwise
*/
public static boolean moveFile(String src, String dest) {
return new File(src).renameTo(new File(dest));
}
/**
* Moves (renames) a file without any check.
*
* @param src source file
* @param dest destination file
*
* @return true if sucess, false otherwise
*/
public static boolean moveFile(File src, File dest) {
return src.renameTo(dest);
}
// ---------------------------------------------------------------- move/copy directory
public static boolean moveDir(String fileIn, String fileOut) {
return moveDir(new File(fileIn), new File(fileOut));
}
/**
* Moves (renames) one folder to another. If source doesn't exist, operation
* fails. If source equals to destination, operaiton is successful. If source
* isn't a folder, operation fails. If destinatione exist, operation fails.
*
* @param fileIn source folder
* @param fileOut destination folder
*
* @return true if success, false otherwise
*/
public static boolean moveDir(File fileIn, File fileOut) {
// check if source exists
if (fileIn.exists() == false) {
return false;
}
// check if source is a directory
if (fileIn.isDirectory() == false) {
return false;
}
// check if destination exists
if (fileOut.exists() == true) {
try {
if (fileIn.getCanonicalFile().equals(fileOut.getCanonicalFile()) == true) {
return true;
} else {
return false;
}
} catch (IOException ioex) {
return false;
}
}
return fileIn.renameTo(fileOut);
}
public static boolean copyDir(String srcDir, String dstDir) {
return copyDir(new File(srcDir), new File(dstDir));
}
/**
* Copies all files under source folder to destination. If destinatio does
* not exist, it will be created.
*
* @param srcDir source
* @param dstDir destination
*
* @return true if success, false otherwise
*/
public static boolean copyDir(File srcDir, File dstDir) {
if (srcDir.isDirectory()) {
if (!dstDir.exists()) {
dstDir.mkdir();
}
String[] files = srcDir.list();
for (int i = 0; i < files.length; i++) {
if (copyDir(new File(srcDir, files[i]), new File(dstDir, files[i])) == false) {
return false;
}
}
return true;
}
return copyFile(srcDir, dstDir);
}
// ---------------------------------------------------------------- delete file/dir
/**
* Deletes files or empty folders, identical to File.delete().
*
* @param fileName name of file to delete
*/
public static boolean delete(String fileName) {
return delete(new File(fileName));
}
/**
* Deletes files or empty folders, identical to File.delete().
*
* @param fileIn file to delete
*/
public static boolean delete(File fileIn) {
return fileIn.delete();
}
/**
* Deletes files and/or complete tree recursively.
*
* @param pathName folder name to delete
*/
public static boolean deleteDir(String pathName) {
return deleteDir(new File(pathName));
}
/**
* Deletes files and/or complete tree recursively. Returns true if all
* deletions were successful. If a deletion fails, the method stops
* attempting to delete and returns false.
*
* @param path folder to delete
*
* @return <code>true</code> if success, <code>false</code> otherwise
*/
public static boolean deleteDir(File path) {
if (path.isDirectory()) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (deleteDir(files[i]) == false) {
return false;
}
}
}
return path.delete();
}
// ---------------------------------------------------------------- string utilities
/**
* Buffer size (32KB) for file string methods.
*/
public static int STRING_BUFFER_SIZE = 32 * 1024;
/**
* Reads file's content into a String. Implicitly assumes that the file is in
* the default encoding.
*
* @param fileName name of the file to read from
*
* @return string with file content or null
* @exception IOException
*/
public static String readString(String fileName) throws IOException {
return readString(new File(fileName), STRING_BUFFER_SIZE);
}
/**
* Reads file's content into a String. Implicitly assumes that the file is in
* the default encoding.
*
* @param fileName name of the file to read from
* @param bufferSize buffer size
*
* @return string with file content or null
* @exception IOException
*/
public static String readString(String fileName, int bufferSize) throws IOException {
return readString(new File(fileName), bufferSize);
}
/**
* Reads file's content into a String. Implicitly assumes that the file is in
* the default encoding.
*
* @param file file to read
*
* @return string with file content or null
* @exception IOException
*/
public static String readString(File file) throws IOException {
return readString(file, STRING_BUFFER_SIZE);
}
/**
* Reads file's content into a String. Implicitly assumes that the file is in
* the default encoding.
*
* @param file file to read
* @param bufferSize buffer size
*
* @return string with file content or null
* @exception IOException
*/
public static String readString(File file, int bufferSize) throws IOException {
long fileLen = file.length();
if (fileLen <= 0L) {
if (file.exists() == true) {
return ""; // empty file
}
return null; // all other file len problems
}
if (fileLen > Integer.MAX_VALUE) { // max String size
throw new IOException("File too big for loading into a String!");
}
FileReader fr = null;
BufferedReader brin = null;
char[] buf = null;
try {
fr = new FileReader(file);
brin = new BufferedReader(fr, bufferSize);
int length = (int) fileLen;
buf = new char[length];
brin.read(buf, 0, length);
} finally {
if (brin != null) {
brin.close();
fr = null;
}
if (fr != null) {
fr.close();
}
}
return new String(buf);
}
/**
* Writes string to a file. Implicitly assumes that the file will be written
* the default encoding.
*
* @param fileName name of the destination file
* @param s source string
*
* @exception IOException
*/
public static void writeString(String fileName, String s) throws IOException {
writeString(new File(fileName), s, STRING_BUFFER_SIZE);
}
/**
* Writes string to a file. Implicitly assumes that the file will be written
* the default encoding.
*
* @param fileName name of the destination file
* @param s source string
* @param bufferSize buffer size
*
* @exception IOException
*/
public static void writeString(String fileName, String s, int bufferSize) throws IOException {
writeString(new File(fileName), s, bufferSize);
}
/**
* Writes string to a file. Implicitly assumes that the file will be written
* the default encoding.
*
* @param file destination file
* @param s source string
*
* @exception IOException
*/
public static void writeString(File file, String s) throws IOException {
writeString(file, s, STRING_BUFFER_SIZE);
}
/**
* Writes string to a file. Implicitly assumes that the file will be written
* the default encoding.
*
* @param file destination file
* @param s source string
* @param bufferSize buffer size
*
* @exception IOException
*/
public static void writeString(File file, String s, int bufferSize) throws IOException {
FileWriter fw = null;
BufferedWriter out = null;
if (s == null) {
return;
}
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -