Structs 二 Web应用中的文件操作(复制和删除文件、目录,读取文本文件)
Structs 2 Web应用中的文件操作(复制和删除文件、目录,读取文本文件)
package auh.server; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * Structs 2 Web应用中的文件操作(复制和删除文件、目录,读取文本文件) * @author 可可 * @param RootDir web的根路径,可以通过setRootDir设置,所有操作均相对与根路径进行的。 */ import org.apache.struts2.ServletActionContext; public class WebFile { private String RootDir; public WebFile(){ this.RootDir=ServletActionContext.getServletContext().getRealPath("/"); } public String getRootDir() { return RootDir; } public void setRootDir(String rootDir) { RootDir = rootDir; } /** * 创建多级目录 * @param ParentDir 为空时只创建subDir目录 * @param subDir ParentDir 不为空时以 / 开头,ParentDir 为空时为决定路径 * @return boolean 是否成功 */ public boolean creatDirs(String ParentDir,String subDir) { if(ParentDir==""){ File f=new File(subDir); if(!f.exists())f.mkdirs(); return true; } File aFile = new File(ParentDir); if (aFile.exists()) { File subFile = new File(ParentDir + subDir); if (!subFile.exists())return subFile.mkdirs(); else return true; } else return false; } /** * 删除目录包括目录中的文件 * @param dir 以 / 开头,相对于根路径的目录 * @return boolean 是否成功 */ public boolean deleteDirs(String dir){ dir=RootDir+dir; File file=new File(dir); if(file.exists()){ String[] filelist=file.list(); for(int i=0;i<filelist.length;i++){ File f=new File(dir+"/"+filelist[i]); if(f.exists())f.delete(); } file.delete(); return true; } else return false; } /** * 删除文件 * @param filename 以 / 开头,相对于根路径的文件 * @return boolean 是否成功 */ public void deleteFile(String filename){ filename=RootDir+filename; File file=new File(filename); if(file.exists())file.delete(); } /** * 复制目录包括目录中的文件 * @param sdir 原始路径,以 / 开头,相对于根路径 * @param tdir 目标路径,以 / 开头,相对于根路径 * @return boolean 是否成功 */ public boolean copyDirs(String sdir,String tdir){ String rsdir=RootDir+sdir; String rtdir=RootDir+tdir; creatDirs("",rtdir); File file=new File(rsdir); if(file.isDirectory()){ String[] filelist=file.list(); for(int i=0;i<filelist.length;i++) copyFile(sdir+"/"+filelist[i],tdir+"/"+filelist[i]); return true; } else return false; } /** * 复制文件 * @param sfilename 源文件名,以 / 开头,相对于根路径 * @param tfilename 目标文件名,以 / 开头,相对于根路径 * @return boolean 是否成功 */ public boolean copyFile(String sfilename,String tfilename){ sfilename=RootDir+sfilename; tfilename=RootDir+tfilename; try{ File sfile=new File(sfilename); if(sfile.exists()){ File tfile=new File(tfilename); FileInputStream freader=new FileInputStream(sfile); FileOutputStream fwriter=new FileOutputStream(tfile); byte[] buff=new byte[1024]; int len=0; while((len=freader.read(buff))>0) fwriter.write(buff,0,len); freader.close(); fwriter.close(); }else return false; }catch(IOException e){ System.out.println(e); return false; } return true; } /** * 读取文本文件 * @param encoding 文本文件的编码方式,例:utf-8 * @param filename 以 / 开头,相对于根路径的文件 * @return String 文件中的字符串 */ public String readFile(String filename,String encoding){ File file=new File(RootDir+filename); try{ FileInputStream freader=new FileInputStream(file); int length=(int)file.length(); byte[] buffer=new byte[length]; freader.read(buffer); String s=new String(buffer,encoding); freader.close(); return s; }catch(IOException e){ System.out.println(e); return null; } } }