文件目录处理工具类

文件目录处理工具类


package fileTest;
/*
* public void listRoot()//列出根目录
* public static void listFiles(String filename)//列出指定文件下的文件及文件夹
* public void listFile(String dir)//列出指定目录下的所有文件
* public void createDir(String name)//创建目录
* public void deleteDir(String name) //删除目录
* public void caeateFile(String name)//创建文件
* public void deleteFile(String name)//删除文件
*
* =============未实现的功能============================
* //修改文件
* //修改目录
* //文件复制
* //文件移动
*
*/
import java.io.File;
import java.io.IOException;

public class FileDemo {
public FileDemo(){

}
//列出根目录
public void listRoot(){
File[] file=File.listRoots();
for(int i=0;i<file.length;i++){
System.out.println(file[i]);
}
}
//列出指定文件下的文件及文件夹
public static void listFiles(String filename){
try
{
File file=new File(filename);
File result[]=file.listFiles();
for(int i=0;i<result.length;i++){
if(result[i].isFile()){
System.out.println("File:"+result[i]);
}else{
System.out.println("DIR:"+result[i]);
// searchFile(result[i].toString());
}
}
}catch(Exception e){
e.printStackTrace();
}
}

//列出指定目录下的所有文件
public void listFile(String dir){
File dirs=new File(dir);
File[] dFile=dirs.listFiles();
for(int i=0;i<dFile.length;i++){
if(dFile[i].isFile()){
System.out.println(dFile[i]);
}
}
}

//根据指定后缀列出文件
//得到文件 后缀
// public String getFileType(String fileUri){
// File file = new File(fileUri);
// String fineName = file.getName();
// String fileType = fileName.substring(fileName.lastIndexOf(\".\")+1,fileName.length());
// return fileType;
// }
// public void findFile(String dir,String hz){
// File dirs=new File(dir);
// File [] dFile=dirs.listFiles();
//
//
//
// }

//创建目录
public void createDir(String name){
File file=new File(name);
file.mkdir();
}
//删除目录
public void deleteDir(String name){
File file=new File(name);
file.delete();
}
//创建文件
public void caeateFile(String name){
File file=new File(name);
try {
file.createNewFile();
System.out.print("--------------Create File Success---------------");
} catch (IOException e) {
System.out.print("--------------Create File fail---------------");
e.printStackTrace();
}
}
//删除文件
public void deleteFile(String name){
File file=new File(name);
if(file.exists()){
file.delete();
}else{
System.out.print("");
}
file.delete();
}

public void listFiles(){

}
public static void main(String[]args){
FileDemo fd=new FileDemo() ;
// fd.listRoot();
// fd.listFiles("c:\\");
fd.listFile("c:\\");



}
}



FileOperation


package SMART.JRSOFT.FILE;

import java.io.*;
import SMART.JRSOFT.UTIL.StringUtils;
import javax.servlet.http.*;

public class FileOperation
{


/**
* 创建本地目录
* @parma dirs 目录名称
* @return 是否成功
* @throws Exception
*/
public boolean makeDir(String dirs) throws Exception
{
boolean result=false;
try
{
File fi=new File(dirs);
//创建目录
result=fi.mkdirs();
}
catch(Exception e)
{
result=false;
System.err.println(e.getMessage());
}
return result;
}

/**
* 创建Web目录的方法
* @param request:Http回应request类
* @param dirs:Web路径 /相对路径 相对于跟目录 例如/Document/Contract/
* @return true/false
* @throws Exception
*/

public boolean makeRemoteDir(HttpServletRequest request,String dirs) throws Exception
{
boolean result=false;
if (dirs != null)
{
String pathString = "";
//得到绝对路径
pathString = request.getRealPath("");
pathString = pathString.replace('\\','/');
//得到目录路径
pathString = pathString + dirs;
try
{
File fi=new File(pathString);
//创建目录
result=fi.mkdirs();
}
catch(Exception e)
{
result=false;
System.err.println(e.getMessage());
}
}
return result;
}


/**
* 删除本地目录
* @parma dirName 目录名称
* @return true/false
*/

public boolean deleteDirectory(String fullDirName)
{
boolean result = false;
int len=0,i=0;
try
{
File Dire=new File(fullDirName);
if (!Dire.exists()) result=false;//源目录不存在返回
if (Dire.isFile())
{
result=Dire.delete();//是文件删除文件
}
File []fi=Dire.listFiles();
len=fi.length;//取得目录下文件和目录数之和
if (len==0)
{
result=Dire.delete();//空目录
}
for (i=0;i<len;i++)
{
if (fi[i].isDirectory())
{
result = deleteDirectory(fi[i].getPath());//删除目录
}
else
{
result = fi[i].delete();//删除文件
}
}
if(Dire.exists())
{
result = Dire.delete();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
return result;
}

/**
* 删除Web目录的方法
* @param request:Http回应request类
* @param filePath:Web路径 /相对路径 相对于跟目录 例如/Document/Contract/
* @return true/false
* @throws Exception
*/

public boolean deleteRemoteDir(HttpServletRequest request,String filePath) throws Exception
{

boolean result = false;
if (filePath != null)
{
String pathString = "";
//取得目录路径
pathString = request.getRealPath("");
pathString = pathString.replace('\\','/');
pathString = pathString + filePath;
int len=0,i=0;
try
{
File Dire=new File(pathString);
if (!Dire.exists())
{
//目录不存在
result = false;
}
if (Dire.isFile())
{
result = Dire.delete();
}
File []fi=Dire.listFiles();
//得到目录下文件数以及文件夹数
len=fi.length;
if (len==0)
{
//删除空目录
result = Dire.delete();
}
for (i=0;i<len;i++)
{
if (fi[i].isDirectory())
{
//删除文件夹以及文件夹所有文件
result=deleteDirectory(fi[i].getPath());
}
else
{
//删除文件
result=fi[i].delete();
}
}
if(Dire.exists())
{
result = Dire.delete();
}
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
}
}
return result;

}

/**
* 移动一个文件夹和文件夹下所有文件
* @parma sourDir 源目录
* @parma desDir 目标目录
* @return 是否成功
*/

public boolean moveDirectory(String sourceDir,String desDir)
{
boolean result=false;
int len=0,i=0;
sourceDir=sourceDir.replace('\\','/');
desDir=desDir.replace('\\','/');
String sourcePath="";
String desPath="";
String fileName="";
try
{
File Dire=new File(sourceDir);
if (!Dire.exists())
{
result=false;
}
File []fi=Dire.listFiles();
len=fi.length;
if (len==0)
{
Dire.delete();//空目录则退出
result=true;
}
File d=new File(desDir);
if(!d.exists())
{
this.makeDir(desDir);
}
for (i=0;i<len;i++)
{
if (fi[i].isDirectory()) //判断是否是子目录
{
//取得子目录名称subdirname
int last = fi[i].getPath().lastIndexOf("\\");
String subdirname = fi[i].getPath().substring(last + 1,
fi[i].getPath().length());
//移动子目录下所有的文件以及目录
result=moveDirectory(fi[i].getPath(), desDir + "/" + subdirname);
if (result)
{
//移除成功删除源目录
deleteDirectory(fi[i].getPath());
}
}
else {
//移除目录下的文件
fileName = fi[i].getName();
sourcePath = fi[i].getAbsolutePath();
desPath = desDir.replace('/', '\\');
desPath = desPath + "\\" + fileName;
this.moveFile(sourcePath, desPath);
}
}
if(Dire.exists())
{
Dire.delete();
return result;
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
return result ;
}


/**
* 移动一个文件夹和文件夹下所有文件
* @parma sourDir 源目录
* @parma desDir 目标目录
* @parma dirName 目标目录指定文件夹的名称
* @return 是否成功
*/
public boolean moveDirectory(String sourceDir,String desDir,String dirName)
{
boolean result=false;
int len=0,i=0;
sourceDir=sourceDir.replace('\\','/');
desDir=desDir.replace('\\','/');
String sourcePath="";
String desPath="";
String fileName="";
try
{
File Dire=new File(sourceDir);
if (!Dire.exists())
{
result=false;
}
File []fi=Dire.listFiles();
len=fi.length;
if (len==0)
{
result=Dire.delete();
}
File d=new File(desDir+"/"+dirName);
if(!d.exists())
{
result=this.makeDir(desDir+"/"+dirName);
}
for (i=0;i<len;i++)
{
if (fi[i].isDirectory())//判断是否是目录
{
//取得子目录的名称subdirName
int last=fi[i].getPath().lastIndexOf("\\");
String subdirName=fi[i].getPath().substring(last+1,fi[i].getPath().length());
//移动子目录以及子目录下文件
result=moveDirectory(fi[i].getPath(), desDir + "/"+dirName+"/"+ subdirName);
if(result)
{
deleteDirectory(fi[i].getPath()); //将源目录删除
}
}
else
{
//移动目录下的文件
fileName=fi[i].getName();
sourcePath=fi[i].getAbsolutePath();
desPath=desDir.replace('/','\\');
desPath=desPath+"\\"+dirName+"\\"+fileName;
result=this.moveFile(sourcePath,desPath);
}
}
//删除源目录
if(Dire.exists())
{
result = Dire.delete();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
return result;
}


/**
* 创建文本文件
* @parma fullFileName 文件路径+文件名称
* @parma txt 文件内容
* @return 是否成功
*/
public boolean createFile(String fullFileName,String txt)
{
boolean restult=false;
try
{
if (txt==null)
txt="";
int last=fullFileName.lastIndexOf("\\");
String dirName=fullFileName.substring(0,last);
File Dire=new File(dirName);
if(!Dire.exists())
{
makeDir(dirName);
}
PrintWriter pw = new PrintWriter(new FileOutputStream(fullFileName));
pw.println(txt);
restult=true;
pw.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return restult;
}


/**
* 创建web文件
* @parma fullFileName 文件路径+文件名称
* @parma txt 文件内容
* @return 是否成功
*/
public boolean createRemoteFile(HttpServletRequest request,String fullFileName,String txt)
{
boolean restult=false;
String pathString="";
pathString = request.getRealPath("");
pathString = pathString.replace('\\','/');
fullFileName=pathString+fullFileName;
try
{
if (txt==null)
txt="";
int last=fullFileName.lastIndexOf("/");
String dirName=fullFileName.substring(0,last);
dirName=dirName.replace('/','\\');
File Dire=new File(dirName);
if(!Dire.exists())
{
makeDir(dirName);
}

PrintWriter pw = new PrintWriter(new FileOutputStream(fullFileName));
pw.println(txt);
restult=true;
pw.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return restult;
}


/**
* 删除本地文件
* @parma fullFileName 文件绝对路径+文件名称 如:D:\\test\\test.jsp
* @return true/false
*/
public boolean deleteFile(String fullFileName)
{
boolean result = false;
File fl;
if ((fullFileName==null)||(fullFileName==""))
{
result = false;
}
fullFileName=StringUtils.replace(fullFileName,"//","\\");
fl=new File(fullFileName);
result=fl.delete();
return result;
}


/**
* 删除web文件的方法
* @param request:Http回应request类
* @param filePath:文件路径 /相对路径 相对于跟目录 例如/Document/Contract/Test.xml
* @return true/false
* @throws Exception
*/
public boolean deleteRemoteFile(HttpServletRequest request,String filePath) throws Exception
{
boolean result=false;
if (filePath != null)
{
String pathString = "";
//取得文件路径以及文件名称
pathString = request.getRealPath("");
pathString = pathString.replace('\\', '/');
pathString = pathString + filePath;
try
{
File f = new File(pathString);
if (f.exists())
result=f.delete(); //删除文件
}
catch (Exception ex)
{

}
}

return result;
}


/**
* 取得文件扩展名
* @parma fullFileName 文件路径+文件名称 文件路径+文件名称或者文件名 如:D:\\test\\test.jsp 或者/test/test.jsp 或者test.jsp
* @return String
*/
public String getFileExtName(String fullFileName)
{
int i=0,Len=0;
String charStr="",rtn="";

if (fullFileName==null) return "";
fullFileName=fullFileName.trim();
Len=fullFileName.length();
if (Len<=1) return "";

for(i=Len-1;i>0;i--)
{
charStr=fullFileName.substring(i,i+1);
rtn=charStr+rtn;
if (charStr.compareTo(".")==0)
break;
}
if (rtn.length()>5)
return "";
else
return rtn;
}


/**
* 取得文件名称不含扩展名
* @parma fullFileName 文件路径+文件名称或者文件名 如:D:\\test\\test.jsp 或者/test/test.jsp 或者test.jsp
* @return String
*/
public String getFileNoExtName(String fullFileName)
{
String rtn="",ext="";
if (fullFileName.length()<=5)
return "";
fullFileName=fullFileName.replace('\\','/');
ext=this.getFileExtName(fullFileName);
int Start=fullFileName.lastIndexOf("/");
rtn=fullFileName.substring(Start+1,fullFileName.length()-ext.length());
return rtn;
}


/**
* 取得完整的文件名包括文件扩展名
* @param str
* @return String
*/

public static final String getFile(String path) {
String result="";
if(path.length()<5)
return "";
try
{
path= path.trim();
String str="";
int i = path.length();
for (i=i;i>0;i--)
{
str=path.substring(i-1,i);
if (str.equals("/") || str.equals("\\"))
break;
result=str+result;
}
}
catch (Exception ex)
{

}
return result;
}


/**
* 判断本地文件或者目录是否存在
* @parma fullFileName 文件路径+文件名称 如:D:\\test\\test.jsp
* @return String
*/
public boolean isExist(String fullFileName)
{
File fl;
if ((fullFileName==null)||(fullFileName==""))
return false;

fullFileName=StringUtils.replace(fullFileName,"//","\\");
fl=new File(fullFileName);
if (fl.exists())
return true;
else
return false;
}


/**
* 判断web目录或者web文件是否存在
* @parma fullFileName 文件路径+文件名称 如:/test/test.jsp
* @return true/false
*/
public boolean isRemoteExist(HttpServletRequest request,String fullFileName)
{
File fl;
if ((fullFileName==null)||(fullFileName==""))
return false;
String pathString = "";
pathString = request.getRealPath("");
pathString = pathString.replace('\\', '/');
pathString = pathString + fullFileName;
fl=new File(pathString);
if (fl.exists())
return true;
else
return false;
}


/**
* 本地文件更名
* @parma oldFileName 文件路径+文件名称 如:D:\\test\\test.jsp newFileName 更改后文件名
* @return true/false
*/
public boolean reName(String oldFileName,String newFileName)
{
boolean result=false;
try
{
File fl;
File f2;
if ((oldFileName==null)||(oldFileName=="")||(newFileName==null)||(newFileName==""))
{
result=false;
}
else
{

if((newFileName.indexOf("\\")>0)||(newFileName.indexOf("/")>0)||(newFileName.indexOf(":")>0)||(newFileName.indexOf("*")>0)||(newFileName.indexOf("?")>0)||(newFileName.indexOf("|")>0)||(newFileName.indexOf("<")>0)||(newFileName.indexOf(">")>0))
{
result=false;
}
else
{
oldFileName = StringUtils.replace(oldFileName, "//", "\\");
int last = oldFileName.lastIndexOf("\\");
String filePath = oldFileName.substring(0, last);
fl = new File(oldFileName);
f2 = new File(filePath + "\\" + newFileName);
result = fl.renameTo(f2);
}
}
}
catch(Exception e)
{
result=false;
System.err.println(e.getMessage());
}
return result;
}


/**
* web目录文件更名
* @parma oldFileName 文件路径+文件名称 如:/test/test.jsp newFileName 更改后文件名
* @return true/false
*/
public boolean reNameRemoteFile(HttpServletRequest request ,String oldFileName,String newFileName)
{
boolean result=false;
try
{
File fl;
File f2;
if ((oldFileName==null)||(oldFileName=="")||(newFileName==null)||(newFileName==""))
{
result=false;
}
else
{
String pathString = "";
if((newFileName.indexOf("\\")>-1)||(newFileName.indexOf("/")>-1)||(newFileName.indexOf(":")>-1)||(newFileName.indexOf("*")>-1)||(newFileName.indexOf("?")>-1)||(newFileName.indexOf("|")>-1)||(newFileName.indexOf("<")>0)||(newFileName.indexOf(">")>-1))
{
result=false;
}
else
{

//取得文件路径以及文件名称
pathString = request.getRealPath("");
pathString = pathString.replace('\\', '/');
pathString = pathString + oldFileName;
int last = pathString.lastIndexOf("/");
//取得路径
String filePath = pathString.substring(0, last);
fl = new File(pathString);
f2 = new File(filePath + "/" + newFileName);
result = fl.renameTo(f2);
}
}
}
catch(Exception e)
{
result=false;
System.err.println(e.getMessage());
}
return result;
}


/**
* 移除文件
* @parma src 源文件地址
* @param des 目的文件地址
* @return true/false
*/
public boolean moveFile(String src,String des) throws Exception
{
boolean result=false;
try
{
FileInputStream fi=new FileInputStream(src);
BufferedInputStream ipt=new BufferedInputStream(fi);

FileOutputStream fo=new FileOutputStream(des);
BufferedOutputStream opt=new BufferedOutputStream(fo);

boolean eof=false;
while (!eof)
{
int input=ipt.read();
if (input==-1)
break;
opt.write(input);
}
ipt.close();
opt.close();
File Source=new File(src);
if (Source.delete())
result=true;
}
catch(Exception e)
{
result=false;
System.err.println(e.getMessage());
}
return result;
}
}





[img]http://dl.iteye.com/upload/attachment/598765/e8b38fa2-9979-365c-beff-b7820842ba1f.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值