⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileopt.java

📁 JAVA FileOperate
💻 JAVA
字号:
//作 者     :朱春平//功 能     :实现文件复制,文件夹复制//编写时间  :2003.1.1package com.WebSiteCreator.FileOpt;import java.io.*;public class FileOpt{    private String str_SrcFolderPath,str_DestFolderPath;    private String str_BasePath;    public FileOpt()    {        this.str_DestFolderPath = "";        this.str_SrcFolderPath ="";        this.str_BasePath="";    }    //CopyFolder 用来复制目录的    public void CopyFolder(String SrcFolder,String DestFolder)    {        this.str_SrcFolderPath = SrcFolder;        this.str_BasePath = SrcFolder;        this.str_DestFolderPath = DestFolder;        this.CreateFolderAndCopyFile();    }    //函数 CopyFile 用来实现把源文件(SrcFile),复制到目标文件(DestFile)    public void CopyFile(String from_name, String to_name)    {       try       {             File from_file = new File(from_name);             File to_file = new File(to_name);             String parent = to_file.getParent();             File dir = new File(parent);             FileInputStream from = null;             FileOutputStream to = null;             try             {                 from = new FileInputStream(from_file);                 to = new FileOutputStream(to_file);                 byte[] buffer = new byte[4096];                 int bytes_read;                 while((bytes_read = from.read(buffer)) != -1)                     to.write(buffer, 0, bytes_read);             }             finally             {                 if (from != null)                 try                 {                       from.close();                 } catch (IOException e) { ; }                 if (to != null)                 try                 {                       to.close();                 } catch (IOException e) { ; }             }       }       catch(java.io.IOException e)       {           java.lang.System.out.println("文件复制出现错误。");       }    }    //复制所有目录,及文件(根据this.str_SrcFolderPath)    boolean CreateFolderAndCopyFile()    {        int i;        String str_temp="",str_src_filename="",str_src_directory="",str_dest_filename="";        java.io.File tmpfile = null;        java.io.File[] fileobjlist = null;        java.io.File foldername = new java.io.File(this.str_SrcFolderPath);        if ( ! foldername.isDirectory())        {             java.lang.System.out.println("Error:" + this.str_SrcFolderPath + "不是目录。");             return false;        }        fileobjlist = foldername.listFiles();        for ( i = 0 ; i < fileobjlist.length ; i++)        {            tmpfile=(java.io.File)fileobjlist[i];            if (tmpfile.isDirectory())            {                this.str_SrcFolderPath = tmpfile.getPath();  //注意this.str_SrcFolderPath 是用来穷举的                str_src_directory = tmpfile.getPath();                str_src_directory = this.str_DestFolderPath + str_src_directory.substring(this.str_BasePath.length());                //str_src_directory = str_src_directory.replace('\\','/')+"/";//标目目录                str_src_directory = str_src_directory +"/";//标目目录                this.CreateDirectory_N(str_src_directory); //建立标目目录                this.CreateFolderAndCopyFile();                continue;            }            str_temp = tmpfile.getPath();            str_src_filename = str_temp.replace('\\','/');  //源文件            str_temp = str_temp.substring(this.str_BasePath.length());            str_temp = this.str_DestFolderPath + str_temp.replace('\\','/');            str_dest_filename  = str_temp; //目标文件            this.CopyFile(str_src_filename,str_dest_filename);        }        return true;    }   //建立目录 1 只能根据目录一级一级的建   public void CreateDirectory(String str_path_1)   {        try        {            java.io.File cdfile = new java.io.File(str_path_1);            if (!cdfile.exists())            {               cdfile.mkdir();            }        }catch(java.lang.Exception e)        {            System.out.print(e.getMessage());        }   }   //建立目录 2 可以连续建立   public void CreateDirectory_N(String str_path_n)   {        try        {              java.io.File  fpFile=null;              String fp = str_path_n,tmpfp="";              String[] fps=fp.split("/");              String sumfp="";              int i,icount;              for (icount = 0 ; icount < fps.length ; icount++)              {                 i = fp.indexOf("/");                 tmpfp = fp.substring(0,i+1);                 fp = fp.replaceAll(tmpfp,"");                 sumfp = sumfp+ tmpfp;                 fpFile = new java.io.File(sumfp);                 if (!fpFile.exists())                 {                      fpFile.mkdir();                 }                 fpFile =null;              }        }catch(java.lang.Exception e)        {            System.out.print(e.getMessage());        }   }}/*函数的调用方法在 jsp 中 :<jsp:useBean id="FileOpt_Bean" class="com.WebSiteCreator.FileOpt.FileOpt" scope="request" /><%FileOpt_Bean.CopyFolder("D:/www/module_man/","D:/www/AA/");   //复制文件夹FileOpt_Bean.CopyFile("D:/module_man/images/find.gif","D:/AA/images/find.gif");  //复制文件夹this.CreateDirectory_N("c:/web/aa/bb/cc/dd/ee/ff/"); //建立标目目录   可以根据目录名 自动建立下去this.CreateDirectory("c:/web/aa/"); //建立标目目录    只能建立一组 注:c:\web\需要存在%>//作 者     :朱春平//功 能     :实现文件复制,文件夹复制//编写时间  :2003.1.1package com.WebSiteCreator.FileOpt;import java.io.*;public class FileOpt{    private String str_SrcFolderPath,str_DestFolderPath;    private String str_BasePath;    public String str_FileNames;    public FileOpt()    {        this.str_DestFolderPath = "";        this.str_SrcFolderPath ="";        this.str_FileNames="";        this.str_BasePath="";    }    //CopyFolder 用来复制目录的    public void CopyFolder(String SrcFolder,String DestFolder)    {        this.str_SrcFolderPath = SrcFolder;        this.str_BasePath = SrcFolder;        this.str_DestFolderPath = DestFolder;        this.CreateFolderAndCopyFile();    }    //函数 CopyFile 用来实现把源文件(SrcFile),复制到目标文件(DestFile)    public void CopyFile(String from_name, String to_name)    {       try       {             File from_file = new File(from_name);             File to_file = new File(to_name);             String parent = to_file.getParent();             File dir = new File(parent);             FileInputStream from = null;             FileOutputStream to = null;             try             {                 from = new FileInputStream(from_file);                 to = new FileOutputStream(to_file);                 byte[] buffer = new byte[4096];                 int bytes_read;                 while((bytes_read = from.read(buffer)) != -1)                     to.write(buffer, 0, bytes_read);             }             finally             {                 if (from != null)                 try                 {                       from.close();                 } catch (IOException e) { ; }                 if (to != null)                 try                 {                       to.close();                 } catch (IOException e) { ; }             }       }       catch(java.io.IOException e)       {           java.lang.System.out.println("文件复制出现错误。");       }    }    //复制所有目录,及文件(根据this.str_SrcFolderPath)    boolean CreateFolderAndCopyFile()    {        int i;        String str_temp="",str_src_filename="",str_src_directory="",str_dest_filename="";        java.io.File tmpfile = null;        java.io.File[] fileobjlist = null;        java.io.File foldername = new java.io.File(this.str_SrcFolderPath);        if ( ! foldername.isDirectory())        {             java.lang.System.out.println("Error:" + this.str_SrcFolderPath + "不是目录。");             return false;        }        fileobjlist = foldername.listFiles();        for ( i = 0 ; i < fileobjlist.length ; i++)        {            tmpfile=(java.io.File)fileobjlist[i];            if (tmpfile.isDirectory())            {                this.str_SrcFolderPath = tmpfile.getPath();  //注意this.str_SrcFolderPath 是用来穷举的                str_src_directory = tmpfile.getPath();                str_src_directory = this.str_DestFolderPath + str_src_directory.substring(this.str_BasePath.length());                //str_src_directory = str_src_directory.replace('\\','/')+"/";//标目目录                str_src_directory = str_src_directory +"/";//标目目录                this.CreateDirectory_N(str_src_directory); //建立标目目录                this.str_FileNames = this.str_FileNames + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                this.str_FileNames = this.str_FileNames + "<font color=red>"+str_src_directory+"</font><br>";                this.CreateFolderAndCopyFile();                this.str_FileNames = this.str_FileNames + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";                continue;            }            str_temp = tmpfile.getPath();            str_src_filename = str_temp.replace('\\','/');  //源文件            this.str_FileNames = this.str_FileNames + "<font color=blue>"+str_src_filename +"</font><br>";            str_temp = str_temp.substring(this.str_BasePath.length());            str_temp = this.str_DestFolderPath + str_temp.replace('\\','/');            str_dest_filename  = str_temp; //目标文件            this.str_FileNames = this.str_FileNames + str_dest_filename +"<br>";            this.CopyFile(str_src_filename,str_dest_filename);        }        return true;    }   public String GetDirectoryFileNameList()   {        return this.str_FileNames;   }   //建立目录 1 只能根据目录一级一级的建   public void CreateDirectory(String str_path_1)   {        try        {            java.io.File cdfile = new java.io.File(str_path_1);            if (!cdfile.exists())            {               cdfile.mkdir();            }        }catch(java.lang.Exception e)        {            System.out.print(e.getMessage());        }   }   //建立目录 2 可以连续建立   public void CreateDirectory_N(String str_path_n)   {        try        {              java.io.File  fpFile=null;              String fp = str_path_n,tmpfp="";              String[] fps=fp.split("/");              String sumfp="";              int i,icount;              for (icount = 0 ; icount < fps.length ; icount++)              {                 i = fp.indexOf("/");                 tmpfp = fp.substring(0,i+1);                 fp = fp.replaceAll(tmpfp,"");                 sumfp = sumfp+ tmpfp;                 fpFile = new java.io.File(sumfp);                 if (!fpFile.exists())                 {                      fpFile.mkdir();                 }                 fpFile =null;              }        }catch(java.lang.Exception e)        {            System.out.print(e.getMessage());        }   }}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -