package cn.edu.tongji.cims.wade.system;  
 
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);  
 
  }  
}

java.io.file的更多相关文章

  1. IO:File类(java.io.File)

    public class File extends Object implements Serializable, Comparable<File> 构造方法: public File(S ...

  2. java获取指定路径下的指定文件/java.io.File.listFiles(FilenameFilter filter)

    java.io.File.listFiles(FilenameFilter filter) 返回抽象路径名数组,表示在目录中此抽象路径名表示,满足指定过滤器的文件和目录. 声明 以下是java.io. ...

  3. 【java IO File】统计项目代码总共多少行

    统计项目代码总共有多少行 思想: 1.首先将不需要迭代的文件夹,保存在集合中,不满足的就是需要迭代的文件夹 2.将需要进行统计行数的代码文件保存在集合中,满足的就是需要计算文件行数的文件 3.迭代方法 ...

  4. java.io.File类

    java.io.File类 1.凡是与输入.输出相关的类.接口等都定义在java.io包下 2.File是一个类.能够有构造器创建其对象.此对象相应着一个文件(.txt .avi .doc .ppt ...

  5. java.io.File类操作

    一.java.io.File类 String path="E:/222/aaa";//路径 String path1="aaa.txt"; File file= ...

  6. 【java】文件操作java.io.File

    package 文件操作; import java.io.File; import java.io.IOException; public class TestFile { public static ...

  7. java.io.File实战

    There are many things that can go wrong: A class works in Unix but doesn't on Windows (or vice versa ...

  8. Java IO file文件的写入和读取及下载

    一.FileWriter 和BufferedWriter 结合写入文件 FileWriter是字符流写入字符到文件.默认情况下,它会使用新的内容代替文件原有的所有内容,但是,当指定一个true值作为F ...

  9. 33.JAVA编程思想——JAVA IO File类

    33.JAVA编程思想--JAVA IO File类 RandomAccessFile用于包括了已知长度记录的文件.以便我们能用 seek()从一条记录移至还有一条:然后读取或改动那些记录. 各记录的 ...

随机推荐

  1. EXTJS4.2中neptune主题的使用

    原文地址:http://blog.csdn.net/xieguojun2013/article/details/8880519 最近在在sencha.com官网了解到EXTJS的最新版本里增加了新的主 ...

  2. code complete part1

    最近在看code complete,学习了一些东西,作为点滴,记录下来. 关于类: 类的接口抽象应该一致 类的接口要可编程,不要对类的使用者做过多的假设.不要出现类似于:A的输入量一定要大于多少小于多 ...

  3. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

  4. Torch 日志文件的保存 logroll

    Torch 日志文件的保存 logroll 怎样将 Torch 在终端显示的信息,保存到 log 文件中 ?   现在介绍一种方法:利用 logroll 的方式.  参考 https://github ...

  5. linux find命令

    Linux中find常见用法示例 ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \; find命令的参数 ...

  6. DDoS

    Distributed Denial of Service (DDoS) Attacks/tools https://staff.washington.edu/dittrich/misc/ddos/ ...

  7. 关于分布式事务的一个误解:使用了TransactionScope就一定会开启分布式事务吗?

    背景: 事务是数据库管理系统的一个基本概念,事务具有四个基本特点,即ACID:原子性(Atomicity).一致性(Consistency).隔离性(Isolation)和持久性(Durability ...

  8. D 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)

    最熟悉的陌生人 作者:张慧桥 “蝶恋花” 我匆匆地跟听众道了声再见,手忙脚乱地关掉了机器,拿出手机按下了一个快捷键…… “嘟…嘟…” 电话响两下后,我听到了那个我在睡梦中都可以认出来的声音. “你现在 ...

  9. 承接Holograms外包 Holograms内容定制 Holograms场景外包开发

    HoloLens仿真器与文档现已向开发者们开放 如何为Microsoft HoloLens全息眼镜开发应用? 每款运行Windows 10的设备都使用了相同统一的Windows内核.所以你学习了所有有 ...

  10. angularJs ng-model/ng-bind

    ng-bind 与ng-model区别ng-bind是从$scope -> view的单向绑定,也就是说ng-bind是相当于{{object.xxx}},是用于展示数据的.ng-modle是$ ...