`
- 浏览:
97717 次
- 来自:
...
-
-
packagecom.work.util;
-
importjava.io.BufferedInputStream;
-
importjava.io.BufferedOutputStream;
-
importjava.io.BufferedReader;
-
importjava.io.BufferedWriter;
-
importjava.io.File;
-
importjava.io.FileInputStream;
-
importjava.io.FileNotFoundException;
-
importjava.io.FileOutputStream;
-
importjava.io.IOException;
-
importjava.io.InputStreamReader;
-
importjava.io.OutputStreamWriter;
-
importjava.io.RandomAccessFile;
-
importjava.io.StringReader;
-
importjava.util.ArrayList;
-
importjava.util.List;
-
importjava.util.zip.ZipEntry;
-
importjava.util.zip.ZipOutputStream;
-
importorg.apache.commons.logging.Log;
-
importorg.apache.commons.logging.LogFactory;
-
-
-
publicclassFileUtil{
-
privatestaticLoglog=LogFactory.getLog(FileUtil.class);
-
-
-
publicstaticvoidcreateDir(Stringdir,booleanignoreIfExitst)
-
throwsIOException{
-
Filefile=newFile(dir);
-
if(ignoreIfExitst&&file.exists()){
-
return;
- }
-
if(file.mkdir()==false){
-
thrownewIOException("Cannotcreatethedirectory="+dir);
- }
- }
-
-
-
publicstaticvoidcreateDirs(Stringdir,booleanignoreIfExitst)
-
throwsIOException{
-
Filefile=newFile(dir);
-
if(ignoreIfExitst&&file.exists()){
-
return;
- }
-
if(file.mkdirs()==false){
-
thrownewIOException("Cannotcreatedirectories="+dir);
- }
- }
-
-
-
publicstaticvoiddeleteFile(Stringfilename)throwsIOException{
-
Filefile=newFile(filename);
-
log.trace("Deletefile="+filename);
-
if(file.isDirectory()){
-
thrownewIOException(
-
"IOException->BadInputException:notafile.");
- }
-
if(file.exists()==false){
-
thrownewIOException(
-
"IOException->BadInputException:fileisnotexist.");
- }
-
if(file.delete()==false){
-
thrownewIOException("Cannotdeletefile.filename="+filename);
- }
- }
-
-
-
publicstaticvoiddeleteDir(Filedir)throwsIOException{
-
if(dir.isFile())
-
thrownewIOException(
-
"IOException->BadInputException:notadirectory.");
- File[]files=dir.listFiles();
-
if(files!=null){
-
for(inti=0;i<files.length;i++){
- Filefile=files[i];
-
if(file.isFile()){
- file.delete();
-
}else{
- deleteDir(file);
- }
- }
-
}
- dir.delete();
- }
-
publicstaticStringgetPathSeparator(){
-
returnjava.io.File.pathSeparator;
- }
-
publicstaticStringgetFileSeparator(){
-
returnjava.io.File.separator;
- }
-
-
-
publicstaticList<FileInfo>getFiles(Filedir)throwsIOException{
-
if(dir.isFile())
-
thrownewIOException("BadInputException:notadirectory.");
-
if(!dir.exists()){
-
thrownewIOException("don'texist");
- }
- File[]files=dir.listFiles();
-
intLEN=0;
-
if(files!=null){
- LEN=files.length;
- }
-
List<FileInfo>l=newArrayList<FileInfo>();
-
longtempFLen=0;
-
for(inti=0;i<LEN;i++){
-
FileInfotemp=newFileInfo();
- temp.setFileName(files[i].getName());
- temp.setIsDir(files[i].isDirectory());
-
-
if(files[i].isFile()){
-
if(files[i].getName().lastIndexOf(".")!=-1)
- temp.setFileType(files[i].getName().substring(
-
files[i].getName().lastIndexOf(".")));
-
}else{
-
temp.setFileType("文件夹");
- }
- tempFLen=files[i].length();
- temp.setFileLen(tempFLen);
-
if(tempFLen/1024/1024/1024>0){
-
temp.setFileLength(files[i].length()/1024/1024/1024+"G");
-
}elseif(tempFLen/1024/1024>0){
-
temp.setFileLength(files[i].length()/1024/1024+"M");
-
}elseif(tempFLen/1024>0){
-
temp.setFileLength(files[i].length()/1024+"K");
-
}else{
-
temp.setFileLength(tempFLen+"byte");
- }
-
temp.setFilePath(files[i].getAbsolutePath().replaceAll("[\\\\]","/"));
- temp.setLastModifiedTime(com.work.util.DateUtil
- .getDateTime(files[i].lastModified()));
- temp.setIsHidden(files[i].isHidden());
-
temp.setAuthor(null);
-
temp.setVersion(null);
-
temp.setFileClass(null);
-
temp.setRemark(null);
- l.add(temp);
- }
-
returnl;
- }
-
-
-
publicstaticlonggetDirLength(Filedir)throwsIOException{
-
if(dir.isFile())
-
thrownewIOException("BadInputException:notadirectory.");
-
longsize=0;
- File[]files=dir.listFiles();
-
if(files!=null){
-
for(inti=0;i<files.length;i++){
- Filefile=files[i];
-
-
-
longlength=0;
-
if(file.isFile()){
- length=file.length();
-
}else{
- length=getDirLength(file);
- }
- size+=length;
-
}
-
}
-
returnsize;
- }
-
-
-
publicstaticvoidemptyFile(StringsrcFilename)throwsIOException{
-
FilesrcFile=newFile(srcFilename);
-
if(!srcFile.exists()){
-
thrownewFileNotFoundException("Cannotfindthefile:"
- +srcFile.getAbsolutePath());
- }
-
if(!srcFile.canWrite()){
-
thrownewIOException("Cannotwritethefile:"
- +srcFile.getAbsolutePath());
- }
-
FileOutputStreamoutputStream=newFileOutputStream(srcFilename);
- outputStream.close();
- }
-
-
-
publicstaticvoidwriteFile(Stringcontent,StringfileName,
-
StringdestEncoding)throwsFileNotFoundException,IOException{
-
Filefile=null;
-
try{
-
file=newFile(fileName);
-
if(!file.exists()){
-
if(file.createNewFile()==false){
-
thrownewIOException("createfile'"+fileName
-
+"'failure.");
- }
- }
-
if(file.isFile()==false){
-
thrownewIOException("'"+fileName+"'isnotafile.");
- }
-
if(file.canWrite()==false){
-
thrownewIOException("'"+fileName+"'isaread-onlyfile.");
- }
-
}finally{
-
- }
-
BufferedWriterout=null;
-
try{
-
FileOutputStreamfos=newFileOutputStream(fileName);
-
out=newBufferedWriter(newOutputStreamWriter(fos,destEncoding));
- out.write(content);
- out.flush();
-
}catch(FileNotFoundExceptionfe){
-
log.error("Error",fe);
-
throwfe;
-
}catch(IOExceptione){
-
log.error("Error",e);
-
throwe;
-
}finally{
-
try{
-
if(out!=null)
- out.close();
-
}catch(IOExceptionex){
- }
- }
- }
-
-
-
publicstaticStringreadFile(StringfileName,StringsrcEncoding)
-
throwsFileNotFoundException,IOException{
-
Filefile=null;
-
try{
-
file=newFile(fileName);
-
if(file.isFile()==false){
-
thrownewIOException("'"+fileName+"'isnotafile.");
- }
-
}finally{
-
- }
-
BufferedReaderreader=null;
-
try{
-
StringBufferresult=newStringBuffer(1024);
-
FileInputStreamfis=newFileInputStream(fileName);
-
reader=newBufferedReader(newInputStreamReader(fis,srcEncoding));
-
char[]block=newchar[512];
-
while(true){
-
intreadLength=reader.read(block);
-
if(readLength==-1)
-
break;
-
result.append(block,0,readLength);
- }
-
returnresult.toString();
-
}catch(FileNotFoundExceptionfe){
-
log.error("Error",fe);
-
throwfe;
-
}catch(IOExceptione){
-
log.error("Error",e);
-
throwe;
-
}finally{
-
try{
-
if(reader!=null)
- reader.close();
-
}catch(IOExceptionex){
- }
- }
- }
-
-
-
publicstaticString[]getLastLines(Filefile,intlinesToReturn)
-
throwsIOException,FileNotFoundException{
-
finalintAVERAGE_CHARS_PER_LINE=250;
-
finalintBYTES_PER_CHAR=2;
-
RandomAccessFilerandomAccessFile=null;
-
StringBufferbuffer=newStringBuffer(linesToReturn
- *AVERAGE_CHARS_PER_LINE);
-
intlineTotal=0;
-
try{
-
randomAccessFile=newRandomAccessFile(file,"r");
-
longbyteTotal=randomAccessFile.length();
-
longbyteEstimateToRead=linesToReturn*AVERAGE_CHARS_PER_LINE
- *BYTES_PER_CHAR;
-
longoffset=byteTotal-byteEstimateToRead;
-
if(offset<0){
-
offset=0;
- }
- randomAccessFile.seek(offset);
-
-
Stringline=null;
-
StringlineUTF8=null;
-
while((line=randomAccessFile.readLine())!=null){
-
lineUTF8=newString(line.getBytes("ISO8859_1"),"UTF-8");
- lineTotal++;
-
buffer.append(lineUTF8).append("\n");
- }
-
}finally{
-
if(randomAccessFile!=null){
-
try{
- randomAccessFile.close();
-
}catch(IOExceptionex){
- }
- }
- }
-
String[]resultLines=newString[linesToReturn];
-
BufferedReaderin=null;
-
try{
-
in=newBufferedReader(newStringReader(buffer.toString()));
-
intstart=lineTotal-linesToReturn;
-
-
if(start<0)
-
start=0;
-
for(inti=0;i<start;i++){
-
in.readLine();
-
- }
-
inti=0;
-
Stringline=null;
-
while((line=in.readLine())!=null){
- resultLines[i]=line;
- i++;
- }
-
}catch(IOExceptionie){
-
log.error("Error"+ie);
-
throwie;
-
}finally{
-
if(in!=null){
-
try{
- in.close();
-
}catch(IOExceptionex){
- }
- }
- }
-
returnresultLines;
- }
-
-
-
publicstaticvoidcopyFile(StringsrcFilename,StringdestFilename,
-
booleanoverwrite)throwsIOException{
-
FilesrcFile=newFile(srcFilename);
-
-
if(!srcFile.exists()){
-
thrownewFileNotFoundException("Cannotfindthesourcefile:"
- +srcFile.getAbsolutePath());
- }
-
-
if(!srcFile.canRead()){
-
thrownewIOException("Cannotreadthesourcefile:"
- +srcFile.getAbsolutePath());
- }
-
FiledestFile=newFile(destFilename);
-
if(overwrite==false){
-
-
if(destFile.exists())
-
return;
-
}else{
-
-
if(destFile.exists()){
-
if(!destFile.canWrite()){
-
thrownewIOException("Cannotwritethedestinationfile:"
- +destFile.getAbsolutePath());
- }
-
}else{
-
-
if(!destFile.createNewFile()){
-
thrownewIOException("Cannotwritethedestinationfile:"
- +destFile.getAbsolutePath());
- }
- }
- }
-
BufferedInputStreaminputStream=null;
-
BufferedOutputStreamoutputStream=null;
-
byte[]block=newbyte[1024];
-
try{
-
inputStream=newBufferedInputStream(newFileInputStream(srcFile));
-
outputStream=newBufferedOutputStream(newFileOutputStream(
- destFile));
-
while(true){
-
intreadLength=inputStream.read(block);
-
if(readLength==-1)
-
break;
-
outputStream.write(block,0,readLength);
- }
-
}finally{
-
if(inputStream!=null){
-
try{
- inputStream.close();
-
}catch(IOExceptionex){
-
- }
- }
-
if(outputStream!=null){
-
try{
- outputStream.close();
-
}catch(IOExceptionex){
-
- }
- }
- }
- }
-
-
-
publicstaticvoidcopyFile(FilesrcFile,FiledestFile,booleanoverwrite)
-
throwsIOException{
-
-
if(!srcFile.exists()){
-
thrownewFileNotFoundException("Cannotfindthesourcefile:"
- +srcFile.getAbsolutePath());
- }
-
-
if(!srcFile.canRead()){
-
thrownewIOException("Cannotreadthesourcefile:"
- +srcFile.getAbsolutePath());
- }
-
if(overwrite==false){
-
-
if(destFile.exists())
-
return;
-
}else{
-
-
if(destFile.exists()){
-
if(!destFile.canWrite()){
-
thrownewIOException("Cannotwritethedestinationfile:"
- +destFile.getAbsolutePath());
- }
-
}else{
-
-
if(!destFile.createNewFile()){
-
thrownewIOException("Cannotwritethedestinationfile:"
- +destFile.getAbsolutePath());
- }
- }
- }
-
BufferedInputStreaminputStream=null;
-
BufferedOutputStreamoutputStream=null;
-
byte[]block=newbyte[1024];
-
try{
-
inputStream=newBufferedInputStream(newFileInputStream(srcFile));
-
outputStream=newBufferedOutputStream(newFileOutputStream(
- destFile));
-
while(true){
-
intreadLength=inputStream.read(block);
-
if(readLength==-1)
-
break;
-
outputStream.write(block,0,readLength);
- }
-
}finally{
-
if(inputStream!=null){
-
try{
- inputStream.close();
-
}catch(IOExceptionex){
-
- }
- }
-
if(outputStream!=null){
-
try{
- outputStream.close();
-
}catch(IOExceptionex){
-
- }
- }
- }
- }
-
-
-
publicstaticvoidcopyFiles(StringsrcDirName,StringdestDirName,
-
booleanoverwrite)throwsIOException{
-
FilesrcDir=newFile(srcDirName);
-
-
if(!srcDir.exists()){
-
thrownewFileNotFoundException(
-
"Cannotfindthesourcedirectory:"
- +srcDir.getAbsolutePath());
- }
-
FiledestDir=newFile(destDirName);
-
if(overwrite==false){
-
if(destDir.exists()){
-
-
}else{
-
if(destDir.mkdirs()==false){
-
thrownewIOException(
-
"Cannotcreatethedestinationdirectories="
- +destDir);
- }
- }
-
}else{
-
-
if(destDir.exists()){
-
-
}else{
-
-
if(destDir.mkdirs()==false){
-
thrownewIOException(
-
"Cannotcreatethedestinationdirectories="
- +destDir);
- }
- }
- }
-
- File[]srcFiles=srcDir.listFiles();
-
if(srcFiles==null||srcFiles.length<1){
-
-
-
return;
- }
-
-
intSRCLEN=srcFiles.length;
-
for(inti=0;i<SRCLEN;i++){
-
-
FiledestFile=newFile(destDirName+File.separator
- +srcFiles[i].getName());
-
-
-
if(srcFiles[i].isFile()){
- copyFile(srcFiles[i],destFile,overwrite);
-
}else{
-
- copyFiles(srcFiles[i].getAbsolutePath(),destDirName
- +File.separator+srcFiles[i].getName(),overwrite);
- }
- }
- }
-
-
-
publicstaticvoidzipFile(StringsrcFilename,StringdestFilename,
-
booleanoverwrite)throwsIOException{
-
FilesrcFile=newFile(srcFilename);
-
-
if(!srcFile.exists()){
-
thrownewFileNotFoundException("Cannotfindthesourcefile:"
- +srcFile.getAbsolutePath());
- }
-
-
if(!srcFile.canRead()){
-
thrownewIOException("Cannotreadthesourcefile:"
- +srcFile.getAbsolutePath());
- }
-
if(destFilename==null||destFilename.trim().equals("")){
-
destFilename=srcFilename+".zip";
-
}else{
-
destFilename+=".zip";
- }
-
FiledestFile=newFile(destFilename);
-
if(overwrite==false){
-
-
if(destFile.exists())
-
return;
-
}else{
-
-
if(destFile.exists()){
-
if(!destFile.canWrite()){
-
thrownewIOException("Cannotwritethedestinationfile:"
- +destFile.getAbsolutePath());
- }
-
}else{
-
-
if(!destFile.createNewFile()){
-
thrownewIOException("Cannotwritethedestinationfile:"
- +destFile.getAbsolutePath());
- }
- }
- }
-
BufferedInputStreaminputStream=null;
-
BufferedOutputStreamoutputStream=null;
-
ZipOutputStreamzipOutputStream=null;
-
byte[]block=newbyte[1024];
-
try{
-
inputStream=newBufferedInputStream(newFileInputStream(srcFile));
-
outputStream=newBufferedOutputStream(newFileOutputStream(destFile));
-
zipOutputStream=newZipOutputStream(outputStream);
-
zipOutputStream.setComment("通过java程序压缩的");
-
ZipEntryzipEntry=newZipEntry(srcFile.getName());
-
zipEntry.setComment("zipEntry通过java程序压缩的");
- zipOutputStream.putNextEntry(zipEntry);
-
while(true){
-
intreadLength=inputStream.read(block);
-
if(readLength==-1)
-
break;
-
zipOutputStream.write(block,0,readLength);
- }
- zipOutputStream.flush();
- zipOutputStream.finish();
-
}finally{
-
if(inputStream!=null){
-
try{
- inputStream.close();
-
}catch(IOExceptionex){
-
- }
- }
-
if(outputStream!=null){
-
<span
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
该工具类专门转对于Java中的数据文件的移动,复制,拷贝等方法,为开发者提供一系列的便捷的操作方法!极大的方便开发者开发!!
一个常用的Java文件操作类FileUtil.java源代码,类中的所有方法都是静态方法,不需要生成此类的实例,这些Java文件操作类主要有修改文件的最后访问时间、判断指定的文件是否存在、创建指定的目录、清空指定目录中的...
public class FileUtil { protected static Logger log = LoggerFactory.getLogger(FileUtil.class); /** * 压缩文件 * @param inputFileName 要压缩的文件或文件夹路径,例如:c:\\a.txt,c:\\a\ * ...
import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /**...
[工具类] 文件操作工具类 .java.txt [工具类] 序列化保存为XML文件的工具类 .java.txt [工具类] 一个压缩工具类.java.txt [工具类] 用java编写简单UDP网络通信程序 .java.txt [工具类] 中文验证.jsp.txt [工具类] ...
[工具类] 文件操作工具类 .java.txt [工具类] 序列化保存为XML文件的工具类 .java.txt [工具类] 一个压缩工具类.java.txt [工具类] 用java编写简单UDP网络通信程序 .java.txt [工具类] 中文验证.jsp.txt [工具类] ...
操作文件的工具类,特别是对于文本文件具有多个高效好用的方法
D:\002 我的工具类\001 流\文件操作整体\FileUtil.java D:\002 我的工具类\001 流\文件操作整体\valid.java D:\002 我的工具类\001 流\文件操作整体2 D:\002 我的工具类\001 流\文件操作整体2\Charsets.java D:\002 ...
java.io.File myFilePath = new java.io.File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace...
java代码-使用java解决文件相关操作——FileUtil的源代码 ——学习参考资料:仅用于个人学习使用!
1.commons-beanutils.jar:提供对Java反射和自省API的包装,主要提供了对于 JavaBean进行各种操作。提供了对于JavaBean进行各种操作,克隆对象,属性等等. 2.commons-digester.jar:方便地将XML文档所定义的元素转化为...
本文实例为大家分享了Android文件操作工具类的具体代码,供大家参考,具体内容如下 贴上我写的一个文件操作工具类,基本上覆盖了各种文件操作: ...1、FileUtil.java package com.ctgu.filemaster.util
FileUtil 是库项目,用于在内部和外部存储中或从内部和外部存储中读取和写入文件(byteArray)。 如何在代码中使用此库 - 用于在内部存储中/从内部存储中读取和写入文件中的字符串 try { FileUtil....
本文实例讲述了Android开发中的文件操作工具类FileUtil。分享给大家供大家参考,具体如下: package com.ymerp.android.tools; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java...
从输入流中读取string,新建一个文件并写入内容,复制一个目录及其子目录、文件到另外一个目录 ,递归删除目录下的所有文件及子目录下所有文件,读取文本文件内容,以行的形式读取....
主要介绍了Java文件操作工具类fileUtil,结合实例形式分析了java针对文件进行读取、增加、删除、修改、复制等操作的相关实现技巧,需要的朋友可以参考下
重写FileUtil中doPaste()方法,显著提高速度,修正拷贝文件时会导致文件不正常的严重Bug 9. 修正其他部分已发现Bug 10. 版本号升级为0.5 <br>尚未实现的功能: 1. 与外界程序的交互 2. 拖拽 3. ...
JFast开发手册1 JFast开发手册1是基于JFinal+LayUI+MySQL的轻量...* org.pp.utils.FileUtil:文件操作工具 * org.pp.utils.ImportDb:从Excel中导入生成数据表 * org.pp.utils.ImportDict:从Excel中导入生成数据字典