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. 深度|OpenAI 首批研究成果聚焦无监督学习,生成模型如何高效的理解世界(附论文)

    本文经机器之心(微信公众号:almosthuman2014)授权转载,禁止二次转载,原文. 选自 Open AI 作者:ANDREJ KARPATHY, PIETER ABBEEL, GREG BRO ...

  2. P1382 光棍组织

    我现在TMD连dfs都不会写了 原题: MM 虽然一辈子只要一个,但是也得早点解决.于是,n 个光棍们自发组成了一个光棍组织(ruffian organization,By Wind 乱译).现在,光 ...

  3. Objective-C数据类型之id,SEL,BOOL,nil,NULL和NSNull

     id id是指向Objective-C对象的指针,等价于C语言中的void*,可以映射任何对象指针指向他,或者映射它指向其他的对象.常见的id类型就是类的delegate属性. SEL SEL类型是 ...

  4. 想让你的java代码更漂亮,用枚举吧

    枚举是java 5之后添加的一个重要特性,这个特性不能提高性能,但是能让java程序员写出更优雅的代码. 我之前看到过挺多介绍java枚举的不错的帖子,我也来参与以下这个话题. 1. 枚举基本用法 / ...

  5. 在Windows上运行InfoPi

    一.安装Python Python官网的下载页面:  https://www.python.org/downloads/ 请下载Python 3.4或以上版本. (Python 3.5已不再支持Win ...

  6. halcon运行版设置

  7. 触发器事件trigger

    修改mysql结束符   delimiter name 触发器语法:     create trigger 触发器名称       after/before  触发时间     //错误  ERROR ...

  8. Java 使用线程方式Thread和Runnable,以及Thread与Runnable的区别

    一. java中实现线程的方式有Thread和Runnable Thread: public class Thread1 extends Thread{ @Override public void r ...

  9. React Native 网络请求

    如下面的Code,分别介绍了GET,POST,以及使用XMLHttpRequest的Get请求. import React, { Component } from 'react'; import { ...

  10. Django+uwsgi+Nginx安装部署

    安装 安装Nginx Nginx是最流行的高性能HTTP服务器. 安装pcre: wget https://sourceforge.net/projects/pcre/files/pcre/8.37/ ...