`

Java读写文件 FileOperate

    博客分类:
  • java
阅读更多

FileOperate.java


import     java.io.*;     
  
public     class     FileOperate     {     
      public     FileOperate()     {     
      }     
  
      /**     
          *     新建目录     
          *     @param     folderPath     String     如     c:/fqf     
          *     @return     boolean     
          */     
      public     void     newFolder(String     folderPath)     {     
              try     {     
                      String     filePath     =     folderPath;     
                      filePath     =     filePath.toString();     
                      java.io.File     myFilePath     =     new     java.io.File(filePath);     
                      if     (!myFilePath.exists())     {     
                              myFilePath.mkdir();     
                      }     
              }     
              catch     (Exception     e)     {     
                      System.out.println( "新建目录操作出错 ");     
                      e.printStackTrace();     
              }     
      }     
  
      /**     
          *     新建文件     
          *     @param     filePathAndName     String     文件路径及名称     如c:/fqf.txt     
          *     @param     fileContent     String     文件内容     
          *     @return     boolean     
          */     
      public     void     newFile(String     filePathAndName,     String     fileContent)     {     
  
              try     {     
                      String     filePath     =     filePathAndName;     
                      filePath     =     filePath.toString();     
                      File     myFilePath     =     new     File(filePath);     
                      if     (!myFilePath.exists())     {     
                              myFilePath.createNewFile();     
                      }     
                      FileWriter     resultFile     =     new     FileWriter(myFilePath);     
                      PrintWriter     myFile     =     new     PrintWriter(resultFile);     
                      String     strContent     =     fileContent;     
                      myFile.println(strContent);     
                      resultFile.close();     
  
              }     
              catch     (Exception     e)     {     
                      System.out.println( "新建目录操作出错 ");     
                      e.printStackTrace();     
  
              }     
  
      }     
  
      /**     
          *     删除文件     
          *     @param     filePathAndName     String     文件路径及名称     如c:/fqf.txt     
          *     @param     fileContent     String     
          *     @return     boolean     
          */     
      public     void     delFile(String     filePathAndName)     {     
              try     {     
                      String     filePath     =     filePathAndName;     
                      filePath     =     filePath.toString();     
                      java.io.File     myDelFile     =     new     java.io.File(filePath);     
                      myDelFile.delete();     
  
              }     
              catch     (Exception     e)     {     
                      System.out.println( "删除文件操作出错 ");     
                      e.printStackTrace();     
  
              }     
  
      }     
  
      /**     
          *     删除文件夹     
          *     @param     filePathAndName     String     文件夹路径及名称     如c:/fqf     
          *     @param     fileContent     String     
          *     @return     boolean     
          */     
      public     void     delFolder(String     folderPath)     {     
              try     {     
                      delAllFile(folderPath);     //删除完里面所有内容     
                      String     filePath     =     folderPath;     
                      filePath     =     filePath.toString();     
                      java.io.File     myFilePath     =     new     java.io.File(filePath);     
                      myFilePath.delete();     //删除空文件夹     
  
              }     
              catch     (Exception     e)     {     
                      System.out.println( "删除文件夹操作出错 ");     
                      e.printStackTrace();     
  
              }     
  
      }     
  
      /**     
          *     删除文件夹里面的所有文件     
          *     @param     path     String     文件夹路径     如     c:/fqf     
          */     
      public     void     delAllFile(String     path)     {     
              File     file     =     new     File(path);     
              if     (!file.exists())     {     
                      return;     
              }     
              if     (!file.isDirectory())     {     
                      return;     
              }     
              String[]     tempList     =     file.list();     
              File     temp     =     null;     
              for     (int     i     =     0;     i     <     tempList.length;     i++)     {     
                      if     (path.endsWith(File.separator))     {     
                              temp     =     new     File(path     +     tempList[i]);     
                      }     
                      else     {     
                              temp     =     new     File(path     +     File.separator     +     tempList[i]);     
                      }     
                      if     (temp.isFile())     {     
                              temp.delete();     
                      }     
                      if     (temp.isDirectory())     {     
                              delAllFile(path+ "/ "+     tempList[i]);//先删除文件夹里面的文件     
                              delFolder(path+ "/ "+     tempList[i]);//再删除空文件夹     
                      }     
              }     
      }     
  
      /**     
          *     复制单个文件     
          *     @param     oldPath     String     原文件路径     如:c:/fqf.txt     
          *     @param     newPath     String     复制后路径     如:f:/fqf.txt     
          *     @return     boolean     
          */     
      public     void     copyFile(String     oldPath,     String     newPath)     {     
              try     {     
                      int     bytesum     =     0;     
                      int     byteread     =     0;     
                      File     oldfile     =     new     File(oldPath);     
                      if     (oldfile.exists())     {     //文件存在时     
                              InputStream     inStream     =     new     FileInputStream(oldPath);     //读入原文件     
                              FileOutputStream     fs     =     new     FileOutputStream(newPath);     
                              byte[]     buffer     =     new     byte[1444];     
                              int     length;     
                              while     (     (byteread     =     inStream.read(buffer))     !=     -1)     {     
                                      bytesum     +=     byteread;     //字节数     文件大小     
                                      System.out.println(bytesum);     
                                      fs.write(buffer,     0,     byteread);     
                              }     
                              inStream.close();     
                      }     
              }     
              catch     (Exception     e)     {     
                      System.out.println( "复制单个文件操作出错 ");     
                      e.printStackTrace();     
  
              }     
  
      }     
  
      /**     
          *     复制整个文件夹内容     
          *     @param     oldPath     String     原文件路径     如:c:/fqf     
          *     @param     newPath     String     复制后路径     如:f:/fqf/ff     
          *     @return     boolean     
          */     
      public     void     copyFolder(String     oldPath,     String     newPath)     {     
  
              try     {     
                      (new     File(newPath)).mkdirs();     //如果文件夹不存在     则建立新文件夹     
                      File     a=new     File(oldPath);     
                      String[]     file=a.list();     
                      File     temp=null;     
                      for     (int     i     =     0;     i     <     file.length;     i++)     {     
                              if(oldPath.endsWith(File.separator)){     
                                      temp=new     File(oldPath+file[i]);     
                              }     
                              else{     
                                      temp=new     File(oldPath+File.separator+file[i]);     
                              }     
  
                              if(temp.isFile()){     
                                      FileInputStream     input     =     new     FileInputStream(temp);     
                                      FileOutputStream     output     =     new     FileOutputStream(newPath     +     "/ "     +     
                                                      (temp.getName()).toString());     
                                      byte[]     b     =     new     byte[1024     *     5];     
                                      int     len;     
                                      while     (     (len     =     input.read(b))     !=     -1)     {     
                                              output.write(b,     0,     len);     
                                      }     
                                      output.flush();     
                                      output.close();     
                                      input.close();     
                              }     
                              if(temp.isDirectory()){//如果是子文件夹     
                                      copyFolder(oldPath+ "/ "+file[i],newPath+ "/ "+file[i]);     
                              }     
                      }     
              }     
              catch     (Exception     e)     {     
                      System.out.println( "复制整个文件夹内容操作出错 ");     
                      e.printStackTrace();     
  
              }     
  
      }     
  
      /**     
          *     移动文件到指定目录     
          *     @param     oldPath     String     如:c:/fqf.txt     
          *     @param     newPath     String     如:d:/fqf.txt     
          */     
      public     void     moveFile(String     oldPath,     String     newPath)     {     
              copyFile(oldPath,     newPath);     
              delFile(oldPath);     
  
      }     
  
      /**     
          *     移动文件到指定目录     
          *     @param     oldPath     String     如:c:/fqf.txt     
          *     @param     newPath     String     如:d:/fqf.txt     
          */     
      public     void     moveFolder(String     oldPath,     String     newPath)     {     
              copyFolder(oldPath,     newPath);     
              delFolder(oldPath);     
  
      }     
}

 

MergeFile.java

import java.io.*;
import java.util.*;


public class MergeFile {

 //private String srcfile;
 private String descfile="E:\\merge";
 MergeFile(){
  
 }
 public void merge(List<File> srcfiles,String descfile){
  if(descfile!=null&&!descfile.isEmpty()){
   this.descfile=descfile;
  }
  File mergefile=new File(descfile);
  if(!mergefile.exists()){
   mergefile.mkdirs();
  }
  
  for(File file:srcfiles){
   if(file!=null){
    for(File fille2:file.listFiles()){
     try {
      recursion( file.getPath(), fille2);
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
   
  }
 }
  
 public void recursion(String root,File vFile) throws IOException{
   if(vFile.isFile()){
   File descfile=new File(getRelativePath(root,vFile.getPath())); 
    
   copy(  vFile,  descfile);
    
   
   }
   else {
    /*System.out.println("         path="+vFile.getPath());
    System.out.println("CanonicalPath="+vFile.getCanonicalPath());
    System.out.println(" absolutePath="+vFile.getAbsolutePath());
    System.out.println(" relativePath="+getRelativePath(root,vFile.getPath()));*/
    File directoryFile=new File(getRelativePath(root,vFile.getPath()));
    if(!directoryFile.exists()){
     directoryFile.mkdir();
    }
    File[] files=vFile.listFiles();
     if(files!=null && files.length>0){
      for(File file:files){
        
       recursion(root,file);
      } 
     }
     else{
      return;
     } 
     
     
     
   }
    
    
 }
    /**     
     *     复制单个文件     
     *     @param     oldPath     String     原文件路径     如:c:/fqf.txt     
     *     @param     newPath     String     复制后路径     如:f:/fqf.txt     
     *     @return     boolean     
     */     
 public     void     copyFile(String     oldPath,     String     newPath)     {     
         try     {     
                 int     bytesum     =     0;     
                 int     byteread     =     0;     
                 File     oldfile     =     new     File(oldPath);     
                 if     (oldfile.exists())     {     //文件存在时     
                         InputStream     inStream     =     new     FileInputStream(oldPath);     //读入原文件     
                         FileOutputStream     fs     =     new     FileOutputStream(newPath);     
                         byte[]     buffer     =     new     byte[1444];     
                         int     length;     
                         while     (     (byteread     =     inStream.read(buffer))     !=     -1)     {     
                                 bytesum     +=     byteread;     //字节数     文件大小     
                                 System.out.println(bytesum);     
                                 fs.write(buffer,     0,     byteread);     
                         }     
                         inStream.close();     
                 }     
         }     
         catch     (Exception     e)     {     
                 System.out.println( "复制单个文件操作出错 ");     
                 e.printStackTrace();    

         }    

 }     
 public void copy(File srcFile,File descFile){
  copyFile(     srcFile.getPath(),           descFile.getPath()); 
  /*FileOutputStream out=null;
  FileInputStream in=null;
  try {
   out=new FileOutputStream(descFile);
   in=new FileInputStream(srcFile);
   byte[] buffer=new byte[1024*8];
   while(in.read(buffer)!=-1){
    out.write(buffer);
    out.flush();
   }
   
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally{
   
   try {
    if(in!=null){
     in.close();
    }
    if(out!=null){
     out.flush();
     out.close();
    }
    
   } catch (IOException e) {
     // TODO Auto-generated catch block
    e.printStackTrace();
   }
    
   
  }*/
  
 }
 public String getRelativePath(String root,String file){
  return descfile+file.substring(root.length());
 }
 public static void main(String[] args){
  List<File> files=new ArrayList<File>();
  /*files.add(new File("E:\\test\\AppFramework.src"));
  files.add(new File("E:\\test\\javaws.src"));
  files.add(new File("E:\\test\\swing-worker.src"));
  files.add(new File("E:\\test\\swingx.src"));
  files.add(new File("E:\\test\\TimingFramework.src"));
  files.add(new File("C:\\Documents and Settings\\Administrator\\桌面\\新建文件夹 (2)\\SwingSet3"));
  */
  files.add(new File("E:\\test2\\a"));
  files.add(new File("E:\\test2\\b"));
   
  new MergeFile().merge(files, "E:\\merge2");
  System.out.println("merge成功!");
 }
 
}

分享到:
评论

相关推荐

    JAVA FileOperate.zip_java_java zip_zip

    JAVA FileOperate

    fileoperate

    主要对文件进行复制、移动、新建等操作。该文件为一个Jar包,点击即可运行

    FileOperate文件管理

    重庆大学11级计算机学院的第一个JAVA实验: In this project, you are required to implement a file manager on the command line. This manager should include the following functions: Create/Delete a ...

    FileOperate.rar_4 3 2 1_MFC读文件_VS2010 MFC_mfc vs2010_vs2010 读写文件

    MFC读写二进制文件,1.VS2010编译通过2.C++3.文件写3个浮点数,英文字符,中文字符4.文件读3个浮点数,英文字符,中文字符5.基于MFC的实例

    FileOperate

    FileOperate

    j2EE文件上传集合

    this by vsk last modify 2010 10 13 一个是批量上传FileUpload文件夹内 一个是jsmart的单个文件上传jsmartupload ...如果不存在此目录请调用com.vsked.fileoperate包下FilesOperate中 createDir方法

    file-Operate.zip_FileOperate

    这是关于文件流操作的程序,简单,很容易理解。

    fileOperate.exe

    男同胞懂的都懂哈......小视频,小电影,查找微信所有文件夹里面的指定...自已写的批量操作子文件助手, 有批量复制指定文件, 批量移动指定文件, 批量删除空文件夹,批量删除指定文件, 同时有源码需要的请在主页另外下载.

    C#封装的常用文件操作类实例

    这个C#类封装了我们经常能用到的文件操作方法,包括读写文件、获取文件扩展名、复制文件、追加内容到文件、删除文件、移动文件、创建目录、递归删除文件及目录、列目录、列文件等,不可多得。 using System; using ...

    php的多功能文件操作类.zip

    class fileoperate { var $path; var $name; var $result;  function creat_file&#40;$path,$name&#41; {  $filename=$path.$name;  if (file_exists($filename))  {  echo "文件已经存在,请...

    07-文件流IO.pdf

    public class FileOperate { #region 删除文件 /// /// 删除文件 /// /// 文件的全路径. /// &lt;returns&gt;bool public static bool DeleteFile&#40;string FileFullPath&#41; { if (File.Exists...

    文本操作方法OpenFile and operateFile

    文本操作方法OpenFile and operateFile is very usful if you want to study C#

    php文件类,代码规范,非常好使

    以下为调用方法 &lt;?php include("./function/... $ss=new fileoperate(); //$ss-&gt;creat_file&#40;"./log/","aa3.txt"&#41;; $ss-&gt;write_file&#40;"./logpath/","ss.txt","this is the file content",1&#41;; ?&gt;

    Utils常用类

    AsyncTaskAssistant异步任务类 CacheUtil缓存 DateUtil日期转换 ...FileOperate文件 HideSoftKeyBoard软键盘 LogUtil日志打印 MyTask线程 NetUtil网络判断 SPUtilsp存储 StorageUtils文件 ToastUtil 弹出

    文件操作类

    关于文件操作的一些常用方法。

    C#基类库大全下载--苏飞版

    mime读取帮助类 QuotedPrintableEncoding mimeEncoding帮助类 9.PDF 转化类 PDFOperation PDFOperation--C#PDF文件操作帮助类 类主要功能有1.构造函数2.私有字段3.设置字体4.设置页面大小 5.实例化文档6.打开...

    C#基类库大全

    FileOperate INIFile 27.序列化 Serialize SerializeHelper 28.压缩解压缩 SharpZip 29.验证码 YZMHelper Captcha 验证码类,一个很个性的验证码类 30.页面辅助类 HTMLHelper UploadEventArgs JavaScriptPlus ...

    給出的文件里的表達式分別進行計算并将結果添加在后面

    對給出的文件里的表達式分別進行計算,分別將結果添加在后面:運行環境 TURBOC。里面用到了栈,还有表达式求值。。。。。

    最新.net通用基础类库 DotNet.Utilities C#开发必备 包括FTP,PDF,SQL,验证码,正则,加密解密,EXCEL等

    │ FileOperate.cs │ INIFile.cs │ ├─时间戳 │ TimeHelper.cs │ ├─条形码 │ BarCodeToHTML.cs │ ├─正则表达式 │ RegexHelper.cs │ ├─汉字转拼音 │ EcanConvertToCh.cs │ PinYin.cs │ ├─...

    office文档转换成swf

    实现百度文库的效果,写了一个进程,用户上传的txt,pdf,office等文档,可以直接转成swf格式,主要类为FileOperate.cs。同时包含了FlashPaper2.2和swftools转换工具。

Global site tag (gtag.js) - Google Analytics