- 浏览: 508603 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (136)
- SOS经典收藏 (9)
- 好文收藏 (3)
- 技术杂烩 (6)
- SQL常用操作 (8)
- J2EE开发错误 (11)
- Java开发点滴_SSH (20)
- 数据库维护 (11)
- 网络相关 (1)
- WEB JS_CSS_DIV (10)
- 加密解密 (1)
- Swing (2)
- C&C++ (8)
- Linux (2)
- 软件工程 (5)
- Util (3)
- 我的实例 (3)
- Office办公常用 (3)
- JSP_Servlet (3)
- 开发手记 (4)
- Java基础 (3)
- Oracle技术 (6)
- 基础文档库 (9)
- 设计模式 (1)
- weblogic8.1启动的时候,项目报错,找不到Class (0)
- Office应用 (1)
- VM虚拟机技术 (1)
- PHP开发日记 (0)
最新评论
-
MoonLord:
其实不需要删文件,修改一个值就好了,参考:https://gi ...
Beyond Compare报应用程序发生错误 不能打开解决办法 -
别拿土豆不当马铃薯:
${pageContext.request.contextPath} JSP取得绝对路径 -
hysunny0923:
不需要DBUtilis.java啦学到很多~感谢分享~~
第一个JSP+Servlet+JavaBean+JDBC示例程序 -
zsxy168:
少了DBUtils.java文件
第一个JSP+Servlet+JavaBean+JDBC示例程序 -
little_demonye:
写得好详细,清楚了很多,太感谢啦
第一个JSP+Servlet+JavaBean+JDBC示例程序
文件目录处理工具类
FileOperation
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; } }
发表评论
-
常用API_WEB
2012-03-22 23:00 805LOG4J API http://logging.apache ... -
JDBC连接工具类
2011-12-01 23:50 1390package com.ins.db; impor ... -
时间日期工具类
2011-12-01 23:47 1382时间日期工具类 package tools; imp ... -
字符编码工具类
2011-12-01 23:46 1173字符编码工具类 package charTools; ... -
常用数值转换
2011-12-01 23:44 1090常用数值转换 package tools; import ... -
正则表达式
2011-04-06 01:23 927正则表达式: 作用:专门用来处理字符串的。 字符串匹配(字 ... -
Oracle常用函数
2011-04-06 01:12 674Oracle 函数大全(字符串函数,数学函数,日期函数,逻辑运 ... -
Linux 常用命令
2011-03-29 01:03 987Linux 基础命令: pwd 用于显示用户当前所在的目录 ...
相关推荐
文件处理工具类,文件处理工具类,文件处理工具类,文件处理工具类
一个公共的文件处理类, 包含大大小小50多个方法, 不用自己处理连接池问题,不用自己关闭文件流, 非常方便, 我一直都用这个类
* 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws ...
很好的文件操作工具类 好东西要分享
对于nc文件处理的工具类
提供java中对文件类的各种基本操作,主要包括获取文件的后缀名称,读取文件内容,写入文件内容,拷贝文件,将文件转换为二进制数组等操作,转换为Blob格式类等操作
fileutil工具类 处理文件流工具 private static File file; /** * 判断文件是否存在 * * @param path * 文件路径 * @return boolean */ public static boolean fileIsExists(String path) { if (path ==...
一个非常好用的csv文件操作工具
图片文件处理工具类
可以处理png格式图片,可以处理透明背景的图片resizePhotoForPng; 加载下载文件loadDownLoadResource; 缓存复杂文本文件cacheMultipartFile; 缓存url文件cacheUrlFile; 缓存MultipartFile复杂文本...
项目使用文件处理工具类
从输入流中读取string,新建一个文件并写入内容,复制一个目录及其子目录、文件到另外一个目录 ,递归删除目录下的所有文件及子目录下所有文件,读取文本文件内容,以行的形式读取....
- QrcodeUtils.java\防止SQL注入和XSS攻击Filter\获取文件绝对路径最后的文件夹名称\加密工具类 - CryptoUtils.java\日期工具类 - DateUtil.java\图片处理工具类 - ImageUtils.java\文件相关操作工具类——FileUtils...
Android开发、Bitmap 压缩相关操作...判断两字符串是否相等 判断两字符串忽略大小写是否相等 null转为长度为0的字符串 返回字符串长度 首字母大写 首字母小写 反转字符串 转化为半角字符 转化为全角字符)等处理工具类
zip文件压缩解压操作工具类. 1.递归压缩 2.压缩一个目录 3.压缩一个文件 4.解压文件
开发中公共类工具的整理,包括token,加密,redis,分页,同一数据,id生成,日期格式工具类,文件处理工具类,正则表达式工具,异常返回等工具类
文件工具类,Http请求工具类,图片处理工具类。Ip工具类。mail工具类,Map工具类,MD5编码工具类,数字工具类,随机数工具类,反射工具类,字符串处理工具类,URL工具类,XML工具类,常用的数据验证工具类
主要功能: 扫描目录下的文件 获取文件后缀名 获取文件名,去除后缀名 转成File 从下载链接中过去File等
C# 程序辅助工具类 // 程序信息// 程序启动器// (暂无)// 程序单开验证// 权限信息// 开机启动项// 数组格式化// 列表内容判断// 列表内容判断(ListTool)// 字节压缩// 人民币格式化// 中文时辰转换// 日期时间...
Base64工具类-字符编码工具类-数据类型转换-日期工具类-Escape中文转码工具类-fastjson工具类-文件工具类-Http工具类-http请求工具类-用于模拟HTTP请求中GET/POST方式 -图片处理工具类-Ip工具类-mail工具类-Map工具...