`
- 浏览:
102031 次
- 来自:
...
-
-
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中用FileUtil实现文件读写 ...通过上述介绍可以看出,`FileUtil` 工具类在Java项目中对于简化文件操作方面具有重要的作用。开发者可以根据具体需求进一步扩展此类的功能,以便更好地服务于项目的需求。
`FileUtil.java`源码中,这些方法通常会使用Java的`java.io`包提供的基本文件操作类,如`File`, `FileInputStream`, `FileOutputStream`, `BufferedReader`, `BufferedWriter`等。通过封装这些基本操作,`FileUtil`...
该工具类专门转对于Java中的数据文件的移动,复制,拷贝等方法,为开发者提供一系列的便捷的操作方法!极大的方便开发者开发!!
一个常用的Java文件操作类FileUtil.java源代码,类中的所有方法都是静态方法,不需要生成此类的实例,这些Java文件操作类主要有修改文件的最后访问时间、判断指定的文件是否存在、创建指定的目录、清空指定目录中的...
public class FileUtil { protected static Logger log = LoggerFactory.getLogger(FileUtil.class); /** * 压缩文件 * @param inputFileName 要压缩的文件或文件夹路径,例如:c:\\a.txt,c:\\a\ * ...
在Java编程领域,FileUtil.java是一个常见的工具类...实际使用中,根据项目需求,FileUtil.java可能会包含更多的定制化功能,以满足各种复杂的文件操作场景。了解并熟练使用这类工具类,对于提升Java开发效率至关重要。
import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /**...
1. **文件操作**:Java的java.io和java.nio包提供了丰富的文件操作API,包括读写、复制、删除等,用于处理本地文件。 2. **网络通信**:如果共享目录位于远程服务器,项目可能采用了Java的Socket编程或基于...
[工具类] 文件操作工具类 .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 [工具类] ...
"FileUtil"是一个Java工具类库,用于处理与文件操作相关的任务。在Java开发中,文件操作是一项常见的任务,例如读取、写入、移动、复制文件等。FileUtil类通常封装了这些基本操作,提供了更简洁、易用的API,以减少...
1. **文件操作**:`FileUtil`类可能会有用于创建新文件的方法,如`createFile(String filePath)`,这个方法会根据指定的路径创建一个新的文件。此外,还可能包含删除文件的方法,如`deleteFile(String filePath)`,...
3. `BaseExcelFileUtil.java`:这个名字暗示它可能是一个基础类,包含了通用的Excel文件操作方法,如打开工作簿、创建工作表、设置单元格样式等。这两个子类(2003和2007版本的文件工具)可能会继承或引用这个基类,...
在Java编程领域,文件操作是常见且重要的任务之一。标题中的"V3FileUtil.rar"表明这是一个关于处理V3格式文件的工具类,而描述提到的是将V3格式的音频文件转换为WAV格式,这涉及到音频文件的编码与解码技术。在Java...
在Java编程语言中,文件操作是一项基础且重要的任务。这篇博文主要探讨了如何使用Java进行常见的...对于实际项目开发,编写一个`FileUtil`工具类是非常常见的做法,这样可以将文件操作封装起来,便于代码的复用和维护。
Java文件操作工具类fileUtil实例 Java文件操作工具类fileUtil实例是一个提供了多种文件操作方法的工具类,包括文件读取、增加、删除、修改、复制等操作。该工具类可以帮助开发者快速实现文件操作功能,提高开发效率...
9. **FileUtil.java**:文件操作工具类,提供读写文件、创建目录、删除文件等方法。例如,`readFileToString(File file, String encoding)`用于读取文件内容,`deleteFile(String filePath)`用于删除文件。 10. **...
在Java中,文件操作主要通过java.io包中的类来完成。例如,`File`类用于表示文件或目录的路径名,`FileWriter`和`BufferedWriter`则用于向文件写入文本内容。`FileUtil`类可能会包含静态方法,这些方法提供了一种...
1. **Java.IO 包**:Java的核心库提供了`java.io`包,它包含了处理输入/输出流的类,这些类用于进行文件操作。如`File`类,用于表示文件或目录的路径名,提供了许多与文件相关的操作。 2. **File 类**:`File`类是...
此外,Java 7引入了`java.nio.file`包,提供了更现代和强大的文件系统操作API。`Paths`类和`Path`接口提供了一种更面向对象的方式来处理路径。例如,`Paths.get("relative", "path").toAbsolutePath()`可以得到一个...