FileUtils如何实现长尾词的智能处理和高效检索?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2583个文字,预计阅读时间需要11分钟。
gistfile1.txt**文件操作工具类**@author xuan@version $Revision: 1.0 $$Date: 2013-9-4 下午7:24:34 $**public abstract class FileUtils { public static final String EMPTY=; public static final long ONE_KB=1024; public ...}**/
/**
* 文件操作工具类
*
* @author xuan
* @version $Revision: 1.0 $, $Date: 2013-9-4 下午7:24:34 $
*/
public abstract class FileUtils {
public static final String EMPTY = "";
public static final long ONE_KB = 1024;
public static final long ONE_MB = ONE_KB * ONE_KB;
public static final long ONE_GB = ONE_KB * ONE_MB;
public static final long ONE_TB = ONE_KB * ONE_GB;
public static final long ONE_PB = ONE_KB * ONE_TB;
public static final long ONE_EB = ONE_KB * ONE_PB;
private static final long FILE_COPY_BUFFER_SIZE = ONE_MB * 30;
/**
* 截取文件的后缀名,例如文件名是xuan.jpg时,返回的后缀名时jpg
*
* @param fileName 文件名
* @return 后缀名
*/
public static String getExtension(String fileName) {
if (null == fileName) {
return EMPTY;
}
int pointIndex = fileName.lastIndexOf(".");
return pointIndex > 0 && pointIndex < fileName.length() ? fileName
.substring(pointIndex + 1).toLowerCase() : EMPTY;
}
// ////////////////////字节写入读出文件方法,一般可以用来写图片,声音等////////////////////
/**
* 字节写入到文件中
*
* @param file 文件
* @param data 字节数据
* @param append 是否从文件后面添加
* @throws IOException
*/
public static void writeByteArrayToFile(File file, byte[] data,
boolean append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
out.write(data);
} finally {
IOUtils.closeQuietly(out);
}
}
/**
* 读出文件中的字节
*
* @param file 文件
* @return 字节数组
* @throws IOException
*/
public static byte[] readFileToByteArray(File file) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.toByteArray(in, file.length());
} finally {
IOUtils.closeQuietly(in);
}
}
// ////////////////////字符串从文件中写入读出方法////////////////////
/**
* 数据写入文件
*
* @param file 要写入的文件
* @param data 数据
* @param encoding 指定编码
* @param append 是否追加
* @throws IOException
*/
public static void writeStringToFile(File file, String data,
String encoding, boolean append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
IOUtils.write(data, out, encoding);
} finally {
IOUtils.closeQuietly(out);
}
}
/**
* 从文件中以指定编码读取成字符串
*
* @param file 文件
* @param encoding 编码
* @return
* @throws IOException
*/
public static String readFileToString(File file, String encoding)
throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.toString(in, encoding);
} finally {
IOUtils.closeQuietly(in);
}
}
// ////////////////////打开输入输出流方法////////////////////
/**
* 打开一个文件的写入流,文件不存在会自动创建
*
* @param file 文件
* @param append 是否以追加的方式写入
* @return
* @throws IOException
*/
public static FileOutputStream openOutputStream(File file, boolean append)
throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file
+ "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file
+ "' cannot be written to");
}
} else {
File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent
+ "' could not be created");
}
}
}
return new FileOutputStream(file, append);
}
/**
* 打开文件输入流,校验友好的提示
*
* @param file 要打开的文件
* @return
* @throws IOException
*/
public static FileInputStream openInputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file
+ "' exists but is a directory");
}
if (file.canRead() == false) {
throw new IOException("File '" + file + "' cannot be read");
}
} else {
throw new FileNotFoundException("File '" + file
+ "' does not exist");
}
return new FileInputStream(file);
}
// ////////////////////文件单位字节友好输出////////////////////
/**
* 友好的显示单位,舍弃有点问题,例如:byteCountToDisplaySize(2047)显示1K,明显是舍弃了
*
* @param size byte单位值
* @return
*/
public static String byteCountToDisplaySize(long size) {
String displaySize;
if (size / ONE_EB > 0) {
displaySize = String.valueOf(size / ONE_EB) + " EB";
} else if (size / ONE_EB > 0) {
displaySize = String.valueOf(size / ONE_EB) + " PB";
} else if (size / ONE_TB > 0) {
displaySize = String.valueOf(size / ONE_TB) + " TB";
} else if (size / ONE_GB > 0) {
displaySize = String.valueOf(size / ONE_GB) + " GB";
} else if (size / ONE_MB > 0) {
displaySize = String.valueOf(size / ONE_MB) + " MB";
} else if (size / ONE_KB > 0) {
displaySize = String.valueOf(size / ONE_KB) + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
// ////////////////////移动文件////////////////////
/**
* 剪切文件夹到文件夹
*
* @param src 源文件夹
* @param destDir 目的文件夹
* @param isCover 存在是否覆盖
* @throws IOException
*/
public static void moveDirectoryToDirectory(File src, File destDir,
boolean isCover) throws IOException {
if (src == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException(
"Destination directory must not be null");
}
if (destDir.exists()) {
if (isCover) {
deleteFileOrDirectoryQuietly(destDir.getPath());
} else {
throw new IOException(
"Destination directory is exists and isCover=false");
}
}
destDir.mkdirs();
if (!destDir.exists()) {
throw new FileNotFoundException("Destination directory '" + destDir
+ "' create failed]");
}
if (!destDir.isDirectory()) {
throw new IOException("Destination '" + destDir
+ "' is not a directory");
}
boolean rename = src.renameTo(destDir);
if (!rename) {
if (destDir.getCanonicalPath().startsWith(src.getCanonicalPath())) {
throw new IOException("Cannot move directory: " + src
+ " to a subdirectory of itself: " + destDir);
}
copyDirectoryToDirectory(src, destDir);
deleteFileOrDirectoryQuietly(src.getPath());
if (src.exists()) {
throw new IOException("Failed to delete original directory '"
+ src + "' after copy to '" + destDir + "'");
}
}
}
/**
* 剪切文件到文件夹
*
* @param srcFile 源文件
* @param destDir 目的文件夹
* @param isCover 存在是否覆盖
* @throws IOException
*/
public static void moveFileToDirectory(File srcFile, File destDir,
boolean isCover) throws IOException {
if (srcFile == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException(
"Destination directory must not be null");
}
if (!destDir.exists()) {
destDir.mkdirs();
}
if (!destDir.exists()) {
throw new FileNotFoundException("Destination directory '" + destDir
+ "' create failed]");
}
if (!destDir.isDirectory()) {
throw new IOException("Destination '" + destDir
+ "' is not a directory");
}
moveFile(srcFile, new File(destDir, srcFile.getName()), isCover);
}
/**
* 剪切文件到文件
*
* @param srcFile 源文件
* @param destFile 目的文件
* @param isCover 存在是否覆盖
* @throws IOException
*/
public static void moveFile(File srcFile, File destFile, boolean isCover)
throws IOException {
if (null == srcFile) {
throw new NullPointerException("Source must not be null");
}
if (null == destFile) {
throw new NullPointerException("Destination must not be null");
}
if (!srcFile.exists()) {
throw new FileNotFoundException("Source '" + srcFile
+ "' does not exist");
}
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' is a directory");
}
if (destFile.isDirectory()) {
throw new IOException("Destination '" + destFile
+ "' is a directory");
}
if (destFile.exists()) {
if (isCover) {
deleteFileOrDirectoryQuietly(destFile.getPath());
} else {
throw new IOException(
"Destination directory is exists and isCover=false");
}
}
boolean rename = srcFile.renameTo(destFile);
if (!rename) {
copyFile(srcFile, destFile);
if (!srcFile.delete()) {
deleteFileOrDirectoryQuietly(destFile.getPath());
throw new IOException("Failed to delete original file '"
+ srcFile + "' after copy to '" + destFile + "'");
}
}
}
// ////////////////////文件夹内容拷贝到指定文件夹中////////////////////
/**
* 拷贝文件夹到文件夹
*
* @param srcDir 源文件夹
* @param destDir 目的文件夹
* @throws IOException
*/
public static void copyDirectoryToDirectory(File srcDir, File destDir)
throws IOException {
copyDirectoryToDirectory(srcDir, new File(destDir, srcDir.getName()),
null);
}
/**
* 拷贝文件夹到文件夹
*
* @param srcDir 源文件夹
* @param destDir 目的文件夹
* @param filter 文件过滤器
* @throws IOException
*/
public static void copyDirectoryToDirectory(File srcDir, File destDir,
FileFilter filter) throws IOException {
if (srcDir == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (srcDir.exists() && !srcDir.isDirectory()) {
throw new IllegalArgumentException("Source '" + destDir
+ "' is not a directory");
}
if (destDir.exists() && !destDir.isDirectory()) {
throw new IllegalArgumentException("Destination '" + destDir
+ "' is not a directory");
}
if (!srcDir.exists()) {
throw new FileNotFoundException("Source '" + srcDir
+ "' does not exist");
}
if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
throw new IOException("Source '" + srcDir + "' and destination '"
+ destDir + "' are the same");
}
// Cater for destination being directory within the source directory
// (see IO-141)
List
本文共计2583个文字,预计阅读时间需要11分钟。
gistfile1.txt**文件操作工具类**@author xuan@version $Revision: 1.0 $$Date: 2013-9-4 下午7:24:34 $**public abstract class FileUtils { public static final String EMPTY=; public static final long ONE_KB=1024; public ...}**/
/**
* 文件操作工具类
*
* @author xuan
* @version $Revision: 1.0 $, $Date: 2013-9-4 下午7:24:34 $
*/
public abstract class FileUtils {
public static final String EMPTY = "";
public static final long ONE_KB = 1024;
public static final long ONE_MB = ONE_KB * ONE_KB;
public static final long ONE_GB = ONE_KB * ONE_MB;
public static final long ONE_TB = ONE_KB * ONE_GB;
public static final long ONE_PB = ONE_KB * ONE_TB;
public static final long ONE_EB = ONE_KB * ONE_PB;
private static final long FILE_COPY_BUFFER_SIZE = ONE_MB * 30;
/**
* 截取文件的后缀名,例如文件名是xuan.jpg时,返回的后缀名时jpg
*
* @param fileName 文件名
* @return 后缀名
*/
public static String getExtension(String fileName) {
if (null == fileName) {
return EMPTY;
}
int pointIndex = fileName.lastIndexOf(".");
return pointIndex > 0 && pointIndex < fileName.length() ? fileName
.substring(pointIndex + 1).toLowerCase() : EMPTY;
}
// ////////////////////字节写入读出文件方法,一般可以用来写图片,声音等////////////////////
/**
* 字节写入到文件中
*
* @param file 文件
* @param data 字节数据
* @param append 是否从文件后面添加
* @throws IOException
*/
public static void writeByteArrayToFile(File file, byte[] data,
boolean append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
out.write(data);
} finally {
IOUtils.closeQuietly(out);
}
}
/**
* 读出文件中的字节
*
* @param file 文件
* @return 字节数组
* @throws IOException
*/
public static byte[] readFileToByteArray(File file) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.toByteArray(in, file.length());
} finally {
IOUtils.closeQuietly(in);
}
}
// ////////////////////字符串从文件中写入读出方法////////////////////
/**
* 数据写入文件
*
* @param file 要写入的文件
* @param data 数据
* @param encoding 指定编码
* @param append 是否追加
* @throws IOException
*/
public static void writeStringToFile(File file, String data,
String encoding, boolean append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
IOUtils.write(data, out, encoding);
} finally {
IOUtils.closeQuietly(out);
}
}
/**
* 从文件中以指定编码读取成字符串
*
* @param file 文件
* @param encoding 编码
* @return
* @throws IOException
*/
public static String readFileToString(File file, String encoding)
throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.toString(in, encoding);
} finally {
IOUtils.closeQuietly(in);
}
}
// ////////////////////打开输入输出流方法////////////////////
/**
* 打开一个文件的写入流,文件不存在会自动创建
*
* @param file 文件
* @param append 是否以追加的方式写入
* @return
* @throws IOException
*/
public static FileOutputStream openOutputStream(File file, boolean append)
throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file
+ "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file
+ "' cannot be written to");
}
} else {
File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent
+ "' could not be created");
}
}
}
return new FileOutputStream(file, append);
}
/**
* 打开文件输入流,校验友好的提示
*
* @param file 要打开的文件
* @return
* @throws IOException
*/
public static FileInputStream openInputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file
+ "' exists but is a directory");
}
if (file.canRead() == false) {
throw new IOException("File '" + file + "' cannot be read");
}
} else {
throw new FileNotFoundException("File '" + file
+ "' does not exist");
}
return new FileInputStream(file);
}
// ////////////////////文件单位字节友好输出////////////////////
/**
* 友好的显示单位,舍弃有点问题,例如:byteCountToDisplaySize(2047)显示1K,明显是舍弃了
*
* @param size byte单位值
* @return
*/
public static String byteCountToDisplaySize(long size) {
String displaySize;
if (size / ONE_EB > 0) {
displaySize = String.valueOf(size / ONE_EB) + " EB";
} else if (size / ONE_EB > 0) {
displaySize = String.valueOf(size / ONE_EB) + " PB";
} else if (size / ONE_TB > 0) {
displaySize = String.valueOf(size / ONE_TB) + " TB";
} else if (size / ONE_GB > 0) {
displaySize = String.valueOf(size / ONE_GB) + " GB";
} else if (size / ONE_MB > 0) {
displaySize = String.valueOf(size / ONE_MB) + " MB";
} else if (size / ONE_KB > 0) {
displaySize = String.valueOf(size / ONE_KB) + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
// ////////////////////移动文件////////////////////
/**
* 剪切文件夹到文件夹
*
* @param src 源文件夹
* @param destDir 目的文件夹
* @param isCover 存在是否覆盖
* @throws IOException
*/
public static void moveDirectoryToDirectory(File src, File destDir,
boolean isCover) throws IOException {
if (src == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException(
"Destination directory must not be null");
}
if (destDir.exists()) {
if (isCover) {
deleteFileOrDirectoryQuietly(destDir.getPath());
} else {
throw new IOException(
"Destination directory is exists and isCover=false");
}
}
destDir.mkdirs();
if (!destDir.exists()) {
throw new FileNotFoundException("Destination directory '" + destDir
+ "' create failed]");
}
if (!destDir.isDirectory()) {
throw new IOException("Destination '" + destDir
+ "' is not a directory");
}
boolean rename = src.renameTo(destDir);
if (!rename) {
if (destDir.getCanonicalPath().startsWith(src.getCanonicalPath())) {
throw new IOException("Cannot move directory: " + src
+ " to a subdirectory of itself: " + destDir);
}
copyDirectoryToDirectory(src, destDir);
deleteFileOrDirectoryQuietly(src.getPath());
if (src.exists()) {
throw new IOException("Failed to delete original directory '"
+ src + "' after copy to '" + destDir + "'");
}
}
}
/**
* 剪切文件到文件夹
*
* @param srcFile 源文件
* @param destDir 目的文件夹
* @param isCover 存在是否覆盖
* @throws IOException
*/
public static void moveFileToDirectory(File srcFile, File destDir,
boolean isCover) throws IOException {
if (srcFile == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException(
"Destination directory must not be null");
}
if (!destDir.exists()) {
destDir.mkdirs();
}
if (!destDir.exists()) {
throw new FileNotFoundException("Destination directory '" + destDir
+ "' create failed]");
}
if (!destDir.isDirectory()) {
throw new IOException("Destination '" + destDir
+ "' is not a directory");
}
moveFile(srcFile, new File(destDir, srcFile.getName()), isCover);
}
/**
* 剪切文件到文件
*
* @param srcFile 源文件
* @param destFile 目的文件
* @param isCover 存在是否覆盖
* @throws IOException
*/
public static void moveFile(File srcFile, File destFile, boolean isCover)
throws IOException {
if (null == srcFile) {
throw new NullPointerException("Source must not be null");
}
if (null == destFile) {
throw new NullPointerException("Destination must not be null");
}
if (!srcFile.exists()) {
throw new FileNotFoundException("Source '" + srcFile
+ "' does not exist");
}
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' is a directory");
}
if (destFile.isDirectory()) {
throw new IOException("Destination '" + destFile
+ "' is a directory");
}
if (destFile.exists()) {
if (isCover) {
deleteFileOrDirectoryQuietly(destFile.getPath());
} else {
throw new IOException(
"Destination directory is exists and isCover=false");
}
}
boolean rename = srcFile.renameTo(destFile);
if (!rename) {
copyFile(srcFile, destFile);
if (!srcFile.delete()) {
deleteFileOrDirectoryQuietly(destFile.getPath());
throw new IOException("Failed to delete original file '"
+ srcFile + "' after copy to '" + destFile + "'");
}
}
}
// ////////////////////文件夹内容拷贝到指定文件夹中////////////////////
/**
* 拷贝文件夹到文件夹
*
* @param srcDir 源文件夹
* @param destDir 目的文件夹
* @throws IOException
*/
public static void copyDirectoryToDirectory(File srcDir, File destDir)
throws IOException {
copyDirectoryToDirectory(srcDir, new File(destDir, srcDir.getName()),
null);
}
/**
* 拷贝文件夹到文件夹
*
* @param srcDir 源文件夹
* @param destDir 目的文件夹
* @param filter 文件过滤器
* @throws IOException
*/
public static void copyDirectoryToDirectory(File srcDir, File destDir,
FileFilter filter) throws IOException {
if (srcDir == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (srcDir.exists() && !srcDir.isDirectory()) {
throw new IllegalArgumentException("Source '" + destDir
+ "' is not a directory");
}
if (destDir.exists() && !destDir.isDirectory()) {
throw new IllegalArgumentException("Destination '" + destDir
+ "' is not a directory");
}
if (!srcDir.exists()) {
throw new FileNotFoundException("Source '" + srcDir
+ "' does not exist");
}
if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
throw new IOException("Source '" + srcDir + "' and destination '"
+ destDir + "' are the same");
}
// Cater for destination being directory within the source directory
// (see IO-141)
List

