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. You need to use a Theme.AppCompat theme (or descendant) with this activity

    经过本人的经验和在网上查找,有2种方法可以解决. 解决步骤已经写到我的公众号,二维码在下面. 欢迎观看我的CSDN学院课程,地址:http://edu.csdn.net/course/detail/2 ...

  2. Oracle数据库导入导出命令总结

    Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中.利用 ...

  3. 关于unity中C#使用WaitForSeconds的方法

    //我有一段这样的代码,我要实现3秒后执行内容,JS是这样写的 function Update () { load (); } function load (){ //等待3秒执行语句 yield W ...

  4. 学习 Local Sensitive Hash

    1. 最近邻法的应用 1.1 Jaccard 相似集 如何定义相似:即相关属性交集的大小,越大则越相似.我们给相似一个数学上的定义:Jaccard 相似集. 集合 \(S\) 与集合 \(T\) 的 ...

  5. linux计划任务

    一.单一计划任务 安装at: # yum -y install at 启动: # /etc/init.d/atd start 查看是否运行: # ps aux | grep atd 创建计划 # at ...

  6. 【引】objective-c,6:Autorelease Pool

    参考博客: http://blog.leichunfeng.com/blog/2015/05/31/objective-c-autorelease-pool-implementation-princi ...

  7. 各种模板(part 1)

    GCD: int gcd(int a,int b) { ?a:gcd(b,a%b); } 快速幂: void work(int x,int y) //x^y { ; ) { ==) ans=ans*x ...

  8. 差分:IncDec Sequence 差分数组

    突然就提到了这个东西,为了不再出现和去年联赛看见二分没学二分痛拿二等第一的情况,就去学了一下,基础还是比较简单的-- 先看一个经典例题: 给定一个长度为n的数列{a1,a2...an},每次可以选择一 ...

  9. js/css在html文档中的引用外部文件方式对比

    包含css样式表和js脚本的最好方式是使用外部文件,因为css/js和html标记文档可以清晰地分离. css的外部引用写在<head></head>中: <head&g ...

  10. JavaScript 介绍

          JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在 ...