这是一个在工作中遇到的文件出类工具类,代码如下:
package cn.feiruqueshui.webdev.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
public class FileUtil {
public static void sortFiles(File[] files) throws Exception {
sortFiles(files, 0, false);
}
public static void sortFiles(File[] files, int type, boolean desc) throws Exception {
final int fType = type;
final boolean fDesc = desc;
Arrays.sort(files, new Comparator<File>() {
Collator collator = Collator.getInstance();
public int compare(File f1, File f2) {
switch (fType) {
case 1:
Long l1 = f1.isDirectory() ? -1 : f1.length();
Long l2 = f2.isDirectory() ? -1 : f2.length();
if (fDesc)
return l2.compareTo(l1);
return l1.compareTo(l2);
case 2:
CollationKey t1 = collator.getCollationKey(f1.isDirectory() ? "0" : "1" + getFileType(f1).toLowerCase());
CollationKey t2 = collator.getCollationKey(f2.isDirectory() ? "0" : "1" + getFileType(f2).toLowerCase());
if (fDesc)
return t2.compareTo(t1);
return t1.compareTo(t2);
case 3:
Long d1 = f1.lastModified();
Long d2 = f2.lastModified();
boolean b1 = f1.isDirectory(),
b2 = f2.isDirectory();
if (b1 && !b2)
d1 = Long.MIN_VALUE;
if (b2 && !b1)
d2 = Long.MIN_VALUE;
if (fDesc)
return d2.compareTo(d1);
return d1.compareTo(d2);
default:
CollationKey k1 = collator.getCollationKey((f1.isDirectory() ? 0 : 1) + f1.getName().toLowerCase());
CollationKey k2 = collator.getCollationKey((f2.isDirectory() ? 0 : 1) + f2.getName().toLowerCase());
if (fDesc)
return k2.compareTo(k1);
return k1.compareTo(k2);
}
}
});
}
public static boolean deleteFolder(File folder) {
File files[] = folder.listFiles();
for (File file : files) {
if (file.isDirectory())
deleteFolder(file);
else if (!file.delete())
return false;
}
if (!folder.delete())
return false;
return true;
}
public static void deleteFile(String path) {
File file = new File(path);
deleteFile(file);
}
/**
* 删除某个文件,若删除不成功,尝试十次
*
* @param file
*/
public static void deleteFile(File file) {
if (file.isFile() && file.exists()) {
int tryCount = 0;
boolean result = false;
while (!result && tryCount++ < 10) {
System.gc();
result = file.delete();
}
}
}
public static File getUniqueFile(File file) {
if (file.exists()) {
String path, dir, name, ext;
int i;
path = file.getAbsolutePath();
dir = extractDir(path);
name = extractFilename(path);
i = name.lastIndexOf('.');
if (i > -1 && i < name.length() - 1) {
ext = name.substring(i);
name = name.substring(0, i);
} else
ext = "";
i = 1;
do {
file = new File(dir, name + i + ext);
i++;
} while (file.exists());
}
return file;
}
public static void copyFile(File source, File dest, boolean autoRename, boolean isCut) throws Exception {
boolean exists = dest.exists();
if (autoRename && exists) {
dest = getUniqueFile(dest);
exists = false;
}
if (!exists)
dest.createNewFile();
FileChannel in = null;
FileChannel out = null;
try {
in = new FileInputStream(source).getChannel();
out = new FileOutputStream(dest).getChannel();
in.transferTo(0, in.size(), out);
} finally {
if (in != null) {
try {
in.close();
} catch (Throwable t) {
}
}
if (out != null)
out.close();
}
dest.setLastModified(source.lastModified());
if (isCut)
source.delete();
}
public static void copyFolder(File source, File dest, boolean autoRename, boolean isCut) throws Exception {
File files[], destFile, destDir;
String name;
boolean exists = dest.exists();
if (autoRename && exists) {
dest = getUniqueFile(dest);
exists = false;
}
if (!exists)
dest.mkdirs();
destDir = dest;
files = source.listFiles();
for (File file : files) {
name = file.getName();
destFile = new File(destDir, name);
if (file.isFile())
copyFile(file, destFile, autoRename, isCut);
else
copyFolder(file, destFile, autoRename, isCut);
}
if (isCut)
source.delete();
}
public static String readText(File file, String charset) throws Exception {
FileInputStream fs = new FileInputStream(file);
try {
int size = (int) file.length();
byte[] bs = new byte[size];
fs.read(bs);
if (StringUtil.isEmpty(charset))
return new String(bs);
else
return new String(bs, charset);
} finally {
fs.close();
}
}
public static String readUtfText(File file) throws Exception {
return readText(file, "utf-8");
}
public static void writeText(File file, String content, String charset) throws Exception {
if (!file.exists())
file.createNewFile();
FileOutputStream os = new FileOutputStream(file);
try {
if (StringUtil.isEmpty(charset))
os.write(content.getBytes());
else
os.write(content.getBytes(charset));
} finally {
os.close();
}
}
public static void writeUtfText(File file, String content) throws Exception {
writeText(file, content, "utf-8");
}
public static void saveStream(InputStream is, File file) throws Exception {
FileOutputStream os = new FileOutputStream(file);
try {
SysUtil.isToOs(is, os);
} finally {
os.close();
}
}
public static String extractFileExt(String fileName) {
if (!StringUtil.isEmpty(fileName)) {
int i = fileName.lastIndexOf('.');
if (i != -1)
return fileName.substring(i + 1);
}
return "";
}
public static String getFileType(File file) {
String type;
try {
type = FileSystemView.getFileSystemView().getSystemTypeDescription(file);
} catch (Throwable e) {
type = null;
}
if (StringUtil.isEmpty(type))
return FileUtil.extractFileExt(file.getName());
else
return type;
}
// 获取文件的扩展名
public static String getExtension(String fileName) {
if (fileName != null && fileName.length() > 0) {
int i = fileName.lastIndexOf(".");
if (i > -1 && i < (fileName.length() - 1)) {
return fileName.substring(i + 1);
}
}
return "";
}
public static String extractFilename(String fileName) {
if (StringUtil.isEmpty(fileName))
return "";
int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
if (p == -1)
return fileName;
else
return fileName.substring(p + 1);
}
public static String extractFilenameNoExt(String fileName) {
String s = extractFilename(fileName);
int i = s.lastIndexOf('.');
if (i != -1)
return s.substring(0, i);
else
return s;
}
public static String getPath(String path) {
return StringUtil.replace(path, "\\", "/");
}
public static String getPath(File file) {
return getPath(file.getAbsolutePath());
}
public static String extractDir(String fileName) {
if (StringUtil.isEmpty(fileName))
return "";
int pos;
fileName = getPath(fileName);
pos = fileName.lastIndexOf('/');
if (pos == -1)
return fileName;
else
return fileName.substring(0, pos);
}
public static boolean isAncestor(File parent, File child) throws Exception {
File p = parent.getCanonicalFile();
File c = child.getCanonicalFile();
while (c != null) {
if (p.equals(c))
return true;
c = c.getParentFile();
}
return false;
}
public static boolean hasSubFile(File file, boolean isDir) {
File[] files = file.listFiles();
if (files == null)
return false;
for (File f : files)
if (!isDir || f.isDirectory())
return true;
return false;
}
/**
*
* @param filePath
* 需要导出文件的路径
* @param response
* @return 0 失败;1 成功
*/
public static int downLoadFile(String filePath, HttpServletResponse response) {
try {
String fileName = "";
if (filePath.lastIndexOf("/") > 0) {
fileName = new String(filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length()));
} else if (filePath.lastIndexOf("\\") > 0) {
fileName = new String(filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.length()));
}
File file = new File(filePath);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "utf-8"));
response.setContentLength((int) file.length());
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int temp;
while ((temp = fis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, temp);
}
fis.close();
return 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}