java文件操作工具类
最近为了修改大量收藏的美剧文件名,用swing写了个小工具,代码是文件处理部分,具体内容如下
packagedatei.steuern;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.RandomAccessFile;
importjava.nio.ByteBuffer;
importjava.nio.channels.FileChannel;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.logging.Level;
importjava.util.logging.Logger;
/**
*
*@authors.watson
*/
publicclassFileTools{
publicFileTools(){
}
/**
*formatPath转义文件目录
*
*@parampath
*@return
*/
publicstaticStringformatPath(Stringpath){
returnpath.replaceAll("\\\\","/");
}
/**
*combainPath文件路径合并
*
*@parameins
*@paramzwei
*@return
*/
privatestaticStringcombainPath(Stringeins,Stringzwei){
Stringdori="";
eins=null==eins?"":formatPath(eins);
zwei=null==zwei?"":formatPath(zwei);
if(!eins.endsWith("/")&&zwei.indexOf("/")!=0){
dori=eins+"/"+zwei;
}else{
dori=(eins+zwei).replaceAll("//","/");
}
returndori;
}
/**
*list2Array列表转换数组
*
*@paramlist
*@return
*/
privatestaticString[]list2Array(Listlist){
Stringarray[]=(String[])list.toArray(newString[list.size()]);
returnarray;
}
/**
*cp复制文件
*
*@paramsource
*@paramdestination
*@paramloop
*@return
*/
publicstaticList<File>cp(Stringsource,Stringdestination,booleanloop){
List<File>list=newArrayList();
try{
FilesrcFile=newFile(source);
FiledesFile=newFile(destination);
list.addAll(cp(srcFile,desFile,loop));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnlist;
}
/**
*cp复制文件
*
*@paramsource
*@paramdestination
*@paramloop
*@return
*/
publicstaticList<File>cp(Filesource,Filedestination,booleanloop){
List<File>list=newArrayList();
try{
if(!source.exists()||source.isDirectory()){
thrownewFileNotFoundException();
}
list.add(cp(source,destination));
if(loop){
String[]subFile=source.list();
for(StringsubPath:subFile){
Stringsrc=combainPath(source.getPath(),subPath);//子文件原文件路径
Stringdes=combainPath(destination.getPath(),subPath);//子文件目标路径
Filesubfile=newFile(src);
if(subfile.isFile()){
list.add(cp(src,des));
}elseif(subfile.isDirectory()&&loop){
list.addAll(cp(src,des,loop));
}
}
}
}catch(Exceptionex){
j2log(null,null,ex);
}
returnlist;
}
/**
*cp单文件复制文件
*
*@paramsource
*@paramdestination
*@return
*/
publicstaticFilecp(Stringsource,Stringdestination){
FiledesFile=null;
try{
FilesrcFile=newFile(source);
desFile=newFile(destination);
desFile=cp(srcFile,desFile);
}catch(Exceptionex){
j2log(null,null,ex);
}
returndesFile;
}
/**
*cp单文件复制文件
*
*@paramsource
*@paramdestination
*@return
*/
publicstaticFilecp(Filesource,Filedestination){
FileInputStreamin=null;
FileOutputStreamout=null;
FileChannelinc=null;
FileChanneloutc=null;
try{
if(!source.exists()||source.isDirectory()){
thrownewFileNotFoundException();
}
if(source.getPath().equals(destination.getPath())){
returnsource;
}
longallbytes=du(source,false);
if(!destination.exists()){
destination.createNewFile();
}
in=newFileInputStream(source.getPath());
out=newFileOutputStream(destination);
inc=in.getChannel();
outc=out.getChannel();
ByteBufferbyteBuffer=null;
longlength=2097152;//基本大小,默认2M
long_2M=2097152;
while(inc.position()<inc.size()){
if(allbytes>(64*length)){//如果文件大小大于128M缓存改为64M
length=32*_2M;
}elseif(allbytes>(32*length)){//如果文件大小大于64M缓存改为32M
length=16*_2M;
}elseif(allbytes>(16*length)){//如果文件大小大于32M缓存改为16M
length=8*_2M;
}elseif(allbytes>(8*length)){//如果文件大小大于16M缓存改为8M
length=4*_2M;
}elseif(allbytes>(4*length)){//如果文件大小大于8M缓存改为4M
length=2*_2M;
}elseif(allbytes>(2*length)){//如果文件大小大于4M缓存改为2M
length=_2M;
}elseif(allbytes>(length)){//如果文件大小大于2M缓存改为1M
length=_2M/2;
}elseif(allbytes<length){//如果文件小于基本大小,直接输出
length=allbytes;
}
allbytes=inc.size()-inc.position();
byteBuffer=ByteBuffer.allocateDirect((int)length);
inc.read(byteBuffer);
byteBuffer.flip();
outc.write(byteBuffer);
outc.force(false);
}
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
try{
if(null!=inc){
inc.close();
}
if(null!=outc){
outc.close();
}
if(null!=in){
in.close();
}
if(null!=out){
out.close();
}
}catch(Exceptionex){
j2log(null,null,ex);
}
}
returndestination;
}
/**
*rename文件重命名
*
*@paramfilePath
*@paramfrom
*@paramto
*@return
*/
publicstaticFilerename(StringfilePath,Stringfrom,Stringto){
FilenewFile=null;
try{
FileoldFile=newFile(combainPath(filePath,from));
newFile=newFile(combainPath(filePath,to));
rename(newFile,oldFile);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnnewFile;
}
/**
*rename文件重命名
*
*@paramto
*@paramfrom
*@return
*/
publicstaticFilerename(Filefrom,Fileto){
try{
StringnewPath=to.getPath();
StringoldPath=from.getPath();
if(!oldPath.equals(newPath)){
if(!to.exists()){
from.renameTo(to);
}
}
}catch(Exceptionex){
j2log(null,null,ex);
}
returnto;
}
/**
*mv移动文件
*
*@paramfileName
*@paramsource
*@paramdestination
*@paramcover
*/
publicstaticvoidmv(StringfileName,Stringsource,Stringdestination,booleancover){
try{
if(!source.equals(destination)){
FileoldFile=newFile(combainPath(source,fileName));
FilenewFile=newFile(combainPath(destination,fileName));
mv(oldFile,newFile,cover);
}
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*mv移动文件
*
*@paramsource
*@paramdestination
*@paramcover
*/
publicstaticvoidmv(Stringsource,Stringdestination,booleancover){
try{
if(!source.equals(destination)){
FileoldFile=newFile(source);
FilenewFile=newFile(destination);
mv(oldFile,newFile,cover);
}
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*mv移动文件
*
*@paramsource
*@paramdestination
*@paramcover
*/
publicstaticvoidmv(Filesource,Filedestination,booleancover){
try{
if(!source.exists()){
thrownewFileNotFoundException();
}
StringBuilderfileName=newStringBuilder(source.getName());
if(!cover&&source.getPath().equals(destination.getPath())){
if(fileName.indexOf(".")>0){
fileName.insert(fileName.lastIndexOf("."),"_副本");
}else{
fileName.append("_副本");
}
cp(source,newFile(combainPath(source.getParent(),fileName.toString())));
}else{
source.renameTo(destination);
}
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*extensions获取文件扩展名信息
*
*@paramfilePath
*@paramfileName
*@return
*/
privatestaticString[]extensions(StringfilePath,StringfileName){
String[]extension={};
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
extensions(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnextension;
}
/**
*extensions获取文件扩展名信息
*
*@paramfullPath
*@return
*/
privatestaticString[]extensions(StringfullPath){
String[]extension={};
try{
Filefile=newFile(fullPath);
extensions(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnextension;
}
/**
*extensions获取文件扩展名信息
*
*@paramfile
*@return
*/
privatestaticString[]extensions(Filefile){
String[]extension={};
try{
if(file.isFile()){
StringfileName=file.getName();
if(fileName.lastIndexOf(".")>=0){
intlastIndex=fileName.lastIndexOf(".");
extension[0]=String.valueOf(lastIndex);//扩展名的“.”的索引
extension[1]=fileName.substring(lastIndex+1);//扩展名
extension[2]=fileName.substring(0,lastIndex);//文件名
}
}
}catch(Exceptionex){
j2log(null,null,ex);
}
returnextension;
}
/**
*ls遍历文件
*
*@paramfilePath
*@paramloop
*@return
*/
publicstaticList<File>ls(StringfilePath,booleanloop){
List<File>list=newArrayList();
try{
Filefile=newFile(filePath);
list.addAll(ls(file,loop));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnlist;
}
/**
*ls遍历文件
*
*@paramfile
*@paramloop
*@return
*/
publicstaticList<File>ls(Filefile,booleanloop){
List<File>list=newArrayList();
try{
list.add(file);
if(!file.isDirectory()){
list.add(file);
}elseif(file.isDirectory()){
File[]subList=file.listFiles();
subList=filesSort(subList,true);
for(FilesubFile:subList){
if(subFile.isDirectory()&&loop){
list.addAll(ls(subFile.getPath(),loop));
}else{
list.add(subFile);
}
}
}
}catch(Exceptionex){
j2log(null,null,ex);
}
returnlist;
}
/**
*filesSort文件排序(默认升序)
*
*@paramparentPath
*@paramsubList
*@return
*/
privatestaticFile[]filesSort(File[]inFiles,booleanasc){
List<String>files=newArrayList();
List<String>dirs=newArrayList();
for(FilesubFile:inFiles){
if(subFile.isDirectory()){
dirs.add(subFile.getPath());
}elseif(subFile.isFile()){
files.add(subFile.getPath());
}
}
String[]fileArray={};
if(files.size()>0){
fileArray=list2Array(files);
Arrays.sort(fileArray);
if(!asc){
Arrays.sort(fileArray,Collections.reverseOrder());
}
}
String[]dirArray={};
if(dirs.size()>0){
dirArray=list2Array(dirs);
Arrays.sort(dirArray);
if(!asc){
Arrays.sort(dirArray,Collections.reverseOrder());
}
}
returnconcat2FileArray(fileArray,dirArray);
}
/**
*concat2FileArray合并文件数组
*
*@paramold1
*@paramold2
*@return
*/
privatestaticFile[]concat2FileArray(String[]old1,String[]old2){
File[]newArray=newFile[old1.length+old2.length];
for(inti=0,n=old1.length;i<n;i++){
newArray[i]=newFile(old1[i]);
}
for(inti=0,j=old1.length,n=(old1.length+old2.length);j<n;i++,j++){
newArray[j]=newFile(old2[i]);
}
returnnewArray;
}
/**
*read读取文本文件
*
*@paramfilePath
*@paramfileName
*@paramcharset
*@return
*/
publicstaticStringBuilderread(StringfilePath,StringfileName,Stringcharset){
StringBuildersb=newStringBuilder();
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
sb.append(FileTools.tail(file,false,0,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*read读取文本文件
*
*@paramfullPath
*@paramcharset
*@return
*/
publicstaticStringBuilderread(StringfullPath,Stringcharset){
StringBuildersb=newStringBuilder();
try{
Filefile=newFile(fullPath);
sb.append(FileTools.tail(file,false,0,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*read读取文本文件
*
*@paramfile
*@paramcharset
*@return
*/
publicstaticStringBuilderread(Filefile,Stringcharset){
StringBuildersb=newStringBuilder();
try{
sb.append(FileTools.tail(file,false,0,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*find读取文本文件指定行
*
*@paramfilePath
*@paramfileName
*@paramline
*@paramcharset
*@return
*/
publicstaticStringBuilderfind(StringfilePath,StringfileName,intline,Stringcharset){
StringBuildersb=newStringBuilder();
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
sb.append(FileTools.tail(file,true,line,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*find读取文本文件指定行
*
*@paramfullPath
*@paramline
*@paramcharset
*@return
*/
publicstaticStringBuilderfind(StringfullPath,intline,Stringcharset){
StringBuildersb=newStringBuilder();
try{
Filefile=newFile(fullPath);
sb.append(FileTools.tail(file,true,line,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*find读取文本文件指定行
*
*@paramfile
*@paramline
*@paramcharset
*@return
*/
publicstaticStringBuilderfind(Filefile,intline,Stringcharset){
StringBuildersb=newStringBuilder();
try{
sb.append(FileTools.tail(file,true,line,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*tail读取文本文件
*
*@paramfilePath
*@paramfileName
*@paramcharset
*@paramfind
*@paramline
*@return
*/
publicstaticStringBuildertail(StringfilePath,StringfileName,booleanfind,intline,Stringcharset){
StringBuildersb=newStringBuilder();
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
sb.append(FileTools.tail(file,find,line,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*tail读取文本文件
*
*@paramfullPath
*@paramcharset
*@paramfind
*@paramline
*@return
*/
publicstaticStringBuildertail(StringfullPath,booleanfind,intline,Stringcharset){
StringBuildersb=newStringBuilder();
try{
Filefile=newFile(fullPath);
sb.append(FileTools.tail(file,find,line,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsb;
}
/**
*tail读取文本文件
*
*@paramfile
*@paramcharset
*@paramfind
*@paramline
*@return
*/
publicstaticStringBuildertail(Filefile,booleanfind,intline,Stringcharset){
StringBuildersb=newStringBuilder();
BufferedReaderbufferReader=null;
if(null==charset||"".equals(charset)){
charset="UTF-8";
}
try{
if(!file.exists()||file.isDirectory()){
thrownewFileNotFoundException();
}
StringfullPath=file.getPath();
bufferReader=newBufferedReader(newInputStreamReader(newFileInputStream(fullPath),charset));
Stringtemp;
for(inti=0;(temp=bufferReader.readLine())!=null;i++){
if(!find||line==i){
sb.append(temp);
}
}
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
if(null!=bufferReader){
try{
bufferReader.close();
}catch(IOExceptionex){
j2log(null,null,ex);
}
}
}
returnsb;
}
/**
*sed读取文本文件
*
*@paramfilePath
*@paramfileName
*@paramcharset
*@return
*/
publicstaticList<String>sed(StringfilePath,StringfileName,Stringcharset){
List<String>list=newArrayList();
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
list.addAll(FileTools.sed(file,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnlist;
}
/**
*sed读取文本文件
*
*@paramfullPath
*@paramcharset
*@return
*/
publicstaticList<String>sed(StringfullPath,Stringcharset){
List<String>list=newArrayList();
try{
Filefile=newFile(fullPath);
list.addAll(FileTools.sed(file,charset));
}catch(Exceptionex){
j2log(null,null,ex);
}
returnlist;
}
/**
*sed读取文本文件
*
*@paramfile
*@paramcharset
*@return
*/
publicstaticList<String>sed(Filefile,Stringcharset){
List<String>list=newArrayList();
BufferedReaderbufferReader=null;
if(null==charset||"".equals(charset)){
charset="UTF-8";
}
try{
if(!file.exists()||file.isDirectory()){
thrownewFileNotFoundException();
}
StringfullPath=file.getPath();
bufferReader=newBufferedReader(newInputStreamReader(newFileInputStream(fullPath),charset));
Stringtemp;
for(inti=0;(temp=bufferReader.readLine())!=null;i++){
list.add(temp);
}
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
if(null!=bufferReader){
try{
bufferReader.close();
}catch(IOExceptionex){
j2log(null,null,ex);
}
}
}
returnlist;
}
/**
*cat读取文本文件
*
*@paramfilePath
*@paramfileName
*@return
*/
publicstaticbyte[]cat(StringfilePath,StringfileName){
byte[]output={};
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
output=FileTools.cat(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnoutput;
}
/**
*cat读取文本文件
*
*@paramfullPath
*@return
*/
publicstaticbyte[]cat(StringfullPath){
byte[]output={};
try{
Filefile=newFile(fullPath);
output=FileTools.cat(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnoutput;
}
/**
*cat读取文本文件
*
*@paramfile
*@return
*/
publicstaticbyte[]cat(Filefile){
InputStreamin=null;
byte[]output={};
try{
if(!file.exists()||file.isDirectory()){
thrownewFileNotFoundException();
}
StringfullPath=file.getPath();
longlength=du(file,false);
long_2M=2097152;
byte[]bytes=newbyte[(int)length];
in=newFileInputStream(fullPath);
for(intcount=0;count!=-1;){
if(length>16*_2M){
length=4*_2M;
}elseif(length>8*_2M){
length=2*_2M;
}elseif(length>4*_2M){
length=_2M;
}elseif(length>2*_2M){
length=_2M/2;
}elseif(length>_2M){
length=_2M/4;
}else{
length=4096;
}
bytes=newbyte[(int)length];
count=in.read(bytes);
output=concatArray(bytes,output);
length=in.available();
}
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
if(null!=in){
try{
in.close();
}catch(Exceptionex){
j2log(null,null,ex);
}
}
}
returnoutput;
}
/**
*合并数组
*
*@paramold1
*@paramold2
*@return
*/
privatestaticbyte[]concatArray(byte[]old1,byte[]old2){
byte[]newArray=newbyte[old1.length+old2.length];
System.arraycopy(old1,0,newArray,0,old1.length);
System.arraycopy(old2,0,newArray,old1.length,old2.length);
returnnewArray;
}
/**
*dd写入文件fullPath内容content
*
*@paramfilePath
*@paramfileName
*@paramcontent
*@paramisAppend
*/
publicstaticvoiddd(StringfilePath,StringfileName,byte[]content,booleanisAppend){
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
FileTools.dd(file,content,isAppend);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*dd写入文件fullPath内容content
*
*@paramfullPath
*@paramcontent
*@paramisAppend
*/
publicstaticvoiddd(StringfullPath,byte[]content,booleanisAppend){
try{
Filefile=newFile(fullPath);
FileTools.dd(file,content,isAppend);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*dd写入文件fullPath内容content
*
*@paramfile
*@paramcontent
*@paramisAppend
*/
publicstaticvoiddd(Filefile,byte[]content,booleanisAppend){
FileOutputStreamfileOutputStream=null;
try{
if(!file.exists()){
file.createNewFile();
}
fileOutputStream=newFileOutputStream(file,isAppend);
fileOutputStream.write(content);
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
try{
if(null!=fileOutputStream){
fileOutputStream.close();
}
}catch(IOExceptionex){
j2log(null,null,ex);
}
}
}
/**
*write写文件内容content到文件fullPath
*
*@paramfilePath
*@paramfileName
*@paramcontent
*/
publicstaticvoidwrite(StringfilePath,StringfileName,Stringcontent){
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
FileTools.write(file,content,true);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*write写文件内容content到文件fullPath
*
*@paramfullPath
*@paramcontent
*/
publicstaticvoidwrite(StringfullPath,Stringcontent){
try{
Filefile=newFile(fullPath);
FileTools.write(file,content,true);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*write写文件内容content到文件fullPath
*
*@paramfile
*@paramcontent
*/
publicstaticvoidwrite(Filefile,Stringcontent){
try{
FileTools.write(file,content,true);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*write写(追加)文件内容content到文件fullPath
*
*@paramfilePath
*@paramfileName
*@paramcontent
*@paramisAppend
*/
publicstaticvoidwrite(StringfilePath,StringfileName,Stringcontent,booleanisAppend){
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
FileTools.write(file,content,isAppend);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*write写(追加)文件内容content到文件fullPath
*
*@paramfullPath
*@paramcontent
*@paramisAppend
*/
publicstaticvoidwrite(StringfullPath,Stringcontent,booleanisAppend){
try{
Filefile=newFile(fullPath);
FileTools.write(file,content,isAppend);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*write写(追加)文件内容content到文件fullPath
*
*@paramfile
*@paramcontent
*@paramisAppend
*/
publicstaticvoidwrite(Filefile,Stringcontent,booleanisAppend){
FileWriterfileWriter=null;
try{
if(!file.exists()){
file.createNewFile();
}
fileWriter=newFileWriter(file.getPath(),isAppend);
fileWriter.write(content);
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
if(null!=fileWriter){
try{
fileWriter.close();
}catch(IOExceptionex){
j2log(null,null,ex);
}
}
}
}
/**
*tail添加文件内容content到文件的index位置
*
*@paramfilePath
*@paramfileName
*@paramcontent
*@paramindex
*/
publicstaticvoidtail(StringfilePath,StringfileName,Stringcontent,longindex){
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
FileTools.tail(file,content,index);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*tail添加文件内容content到文件的index位置
*
*@paramfullPath
*@paramcontent
*@paramindex
*/
publicstaticvoidtail(StringfullPath,Stringcontent,longindex){
try{
Filefile=newFile(fullPath);
FileTools.tail(file,content,index);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*tail添加文件内容content到文件的index位置
*
*@paramfile
*@paramcontent
*@paramindex
*/
publicstaticvoidtail(Filefile,Stringcontent,longindex){
RandomAccessFilerandomAccessFile=null;
try{
if(!file.exists()){
file.createNewFile();
}
randomAccessFile=newRandomAccessFile(file.getPath(),"rw");
randomAccessFile.seek(index);
randomAccessFile.writeBytes(content);
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
if(null!=randomAccessFile){
try{
randomAccessFile.close();
}catch(Exceptionex){
j2log(null,null,ex);
}
}
}
}
/**
*mkdir创建目录
*
*@paramfilePath
*@paramfileName
*@return
*/
publicstaticFilemkdir(StringfilePath,StringfileName){
Filefile=null;
try{
StringfullPath=combainPath(filePath,fileName);
file=newFile(fullPath);
file=mkdir(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnfile;
}
/**
*mkdir创建目录
*
*@paramfullPath
*@return
*/
publicstaticFilemkdir(StringfullPath){
Filefile=null;
try{
file=newFile(fullPath);
file=mkdir(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnfile;
}
/**
*mkdir创建目录
*
*@paramfile
*@return
*/
publicstaticFilemkdir(Filefile){
try{
if(!file.exists()){
file.mkdir();//如果文件夹不存在则创建
}
}catch(Exceptionex){
j2log(null,null,ex);
}
returnfile;
}
/**
*touch创建文件
*
*@paramfilePath
*@paramfileName
*/
publicstaticvoidtouch(StringfilePath,StringfileName){
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
touch(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*touch创建文件
*
*@paramfullPath
*/
publicstaticvoidtouch(StringfullPath){
try{
Filefile=newFile(fullPath);
touch(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*touch创建文件
*
*@paramfile
*/
publicstaticvoidtouch(Filefile){
try{
if(!file.exists()){
file.createNewFile();//如果文件不存在则创建
}
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*rm删除文件
*
*@paramfilePath
*@paramfileName
*/
publicstaticvoidrm(StringfilePath,StringfileName){
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
rm(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*rm删除文件
*
*@paramfullPath
*/
publicstaticvoidrm(StringfullPath){
try{
Filefile=newFile(fullPath);
rm(file);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*rm删除文件
*
*@paramfile
*/
publicstaticvoidrm(Filefile){
try{
if(!file.exists()){
thrownewFileNotFoundException();
}
if(file.isFile()){
file.delete();
}
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*rmdir删除目录
*
*@paramfilePath
*@paramfileName
*@paramloop
*/
publicstaticvoidrmdir(StringfilePath,StringfileName,booleanloop){
try{
StringfullPath=combainPath(filePath,fileName);
Filedir=newFile(fullPath);
rmdir(dir,loop);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*rmdir删除目录
*
*@paramfullPath
*@paramloop
*/
publicstaticvoidrmdir(StringfullPath,booleanloop){
try{
Filedir=newFile(fullPath);
rmdir(dir,loop);
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*rmdir删除目录
*
*@paramdir
*@paramloop
*/
publicstaticvoidrmdir(Filedir,booleanloop){
try{
if(!dir.exists()){
thrownewFileNotFoundException();
}
if(dir.isDirectory()){
File[]files=dir.listFiles();
intlength=files.length;
for(inti=0;i<length&&loop;i++){
if(files[i].isDirectory()){
rmdir(files[i],loop);
}else{
rm(files[i]);
}
}
if(loop||length==0){
dir.delete();
}
}
}catch(Exceptionex){
j2log(null,null,ex);
}
}
/**
*du获取文件实际大小
*
*@paramfilePath
*@paramfileName
*@paramloop
*@return
*/
publicstaticlongdu(StringfilePath,StringfileName,booleanloop){
longsize=0;
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
size=du(file,loop);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsize;
}
/**
*du获取文件实际大小
*
*@paramfilePath
*@paramfileName
*@return
*/
publicstaticlongdu(StringfilePath,StringfileName){
longsize=0;
try{
StringfullPath=combainPath(filePath,fileName);
Filefile=newFile(fullPath);
size=du(file,false);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsize;
}
/**
*du获取文件实际大小
*
*@paramfullPath
*@return
*/
publicstaticlongdu(StringfullPath){
longsize=0;
try{
Filefile=newFile(fullPath);
size=du(file,false);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsize;
}
/**
*du获取文件实际大小
*
*@paramfile
*@return
*/
publicstaticlongdu(Filefile){
longsize=0;
try{
size=du(file,false);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsize;
}
/**
*du获取文件实际大小
*
*@paramfullPath
*@paramloop
*@return
*/
publicstaticlongdu(StringfullPath,booleanloop){
longsize=0;
try{
Filefile=newFile(fullPath);
size=du(file,loop);
}catch(Exceptionex){
j2log(null,null,ex);
}
returnsize;
}
/**
*du获取文件实际大小
*
*@paramfile
*@paramloop
*@return
*/
publicstaticlongdu(Filefile,booleanloop){
FileChannelfileChannel=null;
longsize=0;
try{
if(!file.exists()){
thrownewFileNotFoundException();
}
if(file.isFile()){
FileInputStreamfis=newFileInputStream(file);
fileChannel=fis.getChannel();
size=fileChannel.size();
}elseif(file.isDirectory()){
File[]files=file.listFiles();
intlength=files.length;
for(inti=0;i<length&&loop;i++){
if(files[i].isDirectory()){
du(files[i],loop);
}else{
size+=du(files[i],false);
}
}
}
}catch(Exceptionex){
j2log(null,null,ex);
}finally{
if(null!=fileChannel){
try{
fileChannel.close();
}catch(Exceptionex){
j2log(null,null,ex);
}
}
}
returnsize;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。