package util;
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.InputStream;
import java.io.Serializable;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
/**
* @see 用于文件相关操作的工具类,集成了log4j,需要log4j包的支持。
* @author Herman.Xiong
* @date 2009-12-29
* @version 1.0
*/
public final class FileUtils implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6841417839693317734L;
private FileUtils() {
}
/**
* 得到文件的输入流,如无法定位文件返回null。
*
* @param relativePath
* 文件相对当前应用程序的类加载器的路径。
* @return 文件的输入流。
*/
public static InputStream getResourceStream(String relativePath) {
return Thread.currentThread().getContextClassLoader()
.getResourceAsStream(relativePath);
}
/**
* 关闭输入流。
*
* @param is
* 输入流,可以是null。
*/
public static void closeInputStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
public static void closeFileOutputStream(FileOutputStream fos) {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
/**
* 从文件路径中提取目录路径,如果文件路径不含目录返回null。
*
* @param filePath
* 文件路径。
* @return 目录路径,不以'/'或操作系统的文件分隔符结尾。
*/
public static String extractDirPath(String filePath) {
int separatePos = Math.max(filePath.lastIndexOf('/'), filePath
.lastIndexOf('\\')); // 分隔目录和文件名的位置
return separatePos == -1 ? null : filePath.substring(0, separatePos);
}
/**
* 从文件路径中提取文件名, 如果不含分隔符返回null
* @param filePath
* @return 文件名, 如果不含分隔符返回null
*/
public static String extractFileName(String filePath) {
int separatePos = Math.max(filePath.lastIndexOf('/'), filePath
.lastIndexOf('\\')); // 分隔目录和文件名的位置
return separatePos == -1 ? null : filePath.substring(separatePos + 1, filePath.length());
}
/**
* 按路径建立文件,如已有相同路径的文件则不建立。
*
* @param filePath
* 要建立文件的路径。
* @return 表示此文件的File对象。
* @throws IOException
* 如路径是目录或建文件时出错抛异常。
*/
public static File makeFile(String filePath) {
File file = new File(filePath);
if (file.isFile())
return file;
if (filePath.endsWith("/") || filePath.endsWith("\\"))
try {
throw new IOException(filePath + " is a directory");
} catch (IOException e) {
e.printStackTrace();
}
String dirPath = extractDirPath(filePath); // 文件所在目录的路径
if (dirPath != null) { // 如文件所在目录不存在则先建目录
makeFolder(dirPath);
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
// log4j.info("Folder has been created: " + filePath);
// System.out.println("文件已创建: " + filePath);
return file;
}
/**
* 新建目录,支持建立多级目录
*
* @param folderPath
* 新建目录的路径字符串
* @return boolean,如果目录创建成功返回true,否则返回false
*/
public static boolean makeFolder(String folderPath) {
try {
File myFilePath = new File(folderPath);
if (!myFilePath.exists()) {
myFilePath.mkdirs();
// System.out.println("新建目录为:" + folderPath);
// log4j.info("Create new folder:" + folderPath);
} else {
// System.out.println("目录已经存在: " + folderPath);
// log4j.info("Folder is existed:" + folderPath);
}
} catch (Exception e) {
// System.out.println("新建目录操作出错");
e.printStackTrace();
// log4j.error("Create new folder error: " + folderPath);
return false;
}
return true;
}
/**
* 删除文件
*
* @param filePathAndName
* 要删除文件名及路径
* @return boolean 删除成功返回true,删除失败返回false
*/
public static boolean deleteFile(String filePathAndName) {
try {
File myDelFile = new File(filePathAndName);
if (myDelFile.exists()) {
myDelFile.delete();
// log4j.info("File:" + filePathAndName + " has been deleted!!!");
}
} catch (Exception e) {
e.printStackTrace();
// log4j.error("Error delete file:" + filePathAndName);
return false;
}
return true;
}
/**
* 递归删除指定目录中所有文件和子文件夹
*
* @param path
* 某一目录的路径,如"c:\cs"
* @param ifDeleteFolder
* boolean值,如果传true,则删除目录下所有文件和文件夹;如果传false,则只删除目录下所有文件,子文件夹将保留
*/
public static void deleteAllFile(String path, boolean ifDeleteFolder) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
String temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith("\\") || path.endsWith("/"))
temp = path + tempList[i];
else
temp = path + File.separator + tempList[i];
if ((new File(temp)).isFile()) {
deleteFile(temp);
} else if ((new File(temp)).isDirectory() && ifDeleteFolder) {
deleteAllFile(path + File.separator + tempList[i],
ifDeleteFolder);// 先删除文件夹里面的文件
deleteFolder(path + File.separator + tempList[i]);// 再删除空文件夹
}
}
}
/**
* 删除文件夹,包括里面的文件
*
* @param folderPath
* 文件夹路径字符串
*/
public static void deleteFolder(String folderPath) {
try {
File myFilePath = new File(folderPath);
if (myFilePath.exists()) {
deleteAllFile(folderPath, true); // 删除完里面所有内容
myFilePath.delete(); // 删除空文件夹
}
// log4j.info("ok!Delete folder success: " + folderPath);
} catch (Exception e) {
e.printStackTrace();
// log4j.error("Delete folder fail: " + folderPath);
}
}
/**
* 复制文件,如果目标文件的路径不存在,会自动新建路径
*
* @param sourcePath
* 源文件路径, e.g. "c:/cs.txt"
* @param targetPath
* 目标文件路径 e.g. "f:/bb/cs.txt"
*/
public static void copyFile(String sourcePath, String targetPath) {
InputStream inStream = null;
FileOutputStream fos = null;
try {
int byteSum = 0;
int byteRead = 0;
File sourcefile = new File(sourcePath);
if (sourcefile.exists()) { // 文件存在时
inStream = new FileInputStream(sourcePath); // 读入原文件
String dirPath = extractDirPath(targetPath); // 文件所在目录的路径
if (dirPath != null) { // 如文件所在目录不存在则先建目录
makeFolder(dirPath);
}
fos = new FileOutputStream(targetPath);
byte[] buffer = new byte[1444];
while ((byteRead = inStream.read(buffer)) != -1) {
byteSum += byteRead; // 字节数 文件大小
fos.write(buffer, 0, byteRead);
}
System.out.println("File size is: " +
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
demo.zip (79个子文件)
demo
bin
crawler
search
SearchBKeyword.class 3KB
timerManager
SearchTask.class 552B
TimerManager.class 573B
log4j.properties 694B
system
LoadConfig.class 3KB
main
AppletClient.class 771B
util
FileUtils.class 16KB
XmlUtil.class 6KB
config
demoConfig.xml 183B
demo.log 7KB
.settings
org.eclipse.core.resources.prefs 88B
src
crawler
search
SearchBKeyword.java 2KB
timerManager
SearchTask.java 472B
TimerManager.java 349B
log4j.properties 694B
system
LoadConfig.java 2KB
main
AppletClient.java 417B
util
FileUtils.java 25KB
XmlUtil.java 12KB
.project 380B
.classpath 3KB
lib
.sigar_shellrc 2KB
solr-solrj-3.4.0.jar 275KB
htmlunit-2.13.jar 1.23MB
fastjson-1.1.24.jar 344KB
commons-collections-3.2.1.jar 562KB
commons-beanutils.jar 160KB
cssparser-0.9.11.jar 346KB
proxool-cglib.jar 327KB
mysql-connector-java-5.1.18.jar 771KB
log4j-1.2.16.jar 470KB
libsigar-ia64-hpux-11.sl 564KB
junit.jar 118KB
xml-apis-1.4.01.jar 215KB
sac-1.3.jar 15KB
msbase.jar 341KB
mail.jar 451KB
org.json-20120521.jar 39KB
libsigar-universal64-macosx.dylib 384KB
httpmime-4.3.1.jar 36KB
commons-httpclient-3.1.jar 298KB
ezmorph-1.0.3.jar 76KB
httpclient-4.3.1.jar 572KB
commons-digester-2.0.jar 145KB
msutil.jar 58KB
sigar-x86-winnt.dll 252KB
slf4j-api-1.6.4.jar 25KB
sigar.jar 413KB
httpcore-4.3.jar 276KB
xercesImpl-2.11.0.jar 1.3MB
commons-logging-1.1.1.jar 59KB
json-lib-2.2.3-jdk15.jar 145KB
jaxen-1.1.3.jar 218KB
mailapi.jar 255KB
libsigar-pa-hpux-11.sl 504KB
libsigar-universal-macosx.dylib 369KB
nekohtml-1.9.19.jar 122KB
htmlunit-core-js-2.13.jar 1009KB
commons-codec-1.5.jar 71KB
sigar-amd64-winnt.dll 384KB
mmseg4j-1.8.5.1-dic.jar 850KB
snmp4j-2.0.2.jar 415KB
slf4j-log4j12-1.6.4.jar 10KB
netty-3.2.1.Final.jar 766KB
serializer-2.7.1.jar 272KB
summary.jar 3.38MB
jzsms-6.0.jar 11KB
hadoop-core-1.0.3.jar 3.75MB
sigar-x86-winnt.lib 96KB
commons-net-1.4.1.jar 177KB
smtp.jar 51KB
commons-lang-2.3.jar 240KB
dom4j-2.0.0-ALPHA-2.jar 332KB
commons-io-2.4.jar 181KB
commons-lang3-3.1.jar 308KB
joda-time-1.6.2.jar 530KB
xalan-2.7.1.jar 3.03MB
ik2012lucene4.jar 47KB
jsoup-1.7.2.jar 287KB
共 79 条
- 1
资源评论
baiyan111
- 粉丝: 20
- 资源: 80
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功