JAVA文件操作类和文件夹的操作(转帖)

  1. package com.gamvan.tools;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.PrintWriter;
  11. import java.util.StringTokenizer;
  12. public class FileOperate {
  13.     private String message;
  14.     public FileOperate() {
  15.     } 
  16.     /**
  17.      * 读取文本文件内容
  18.      * @param filePathAndName 带有完整绝对路径的文件名
  19.      * @param encoding 文本文件打开的编码方式
  20.      * @return 返回文本文件的内容
  21.      */
  22.     public String readTxt(String filePathAndName,String encoding) throws IOException{
  23.      encoding = encoding.trim();
  24.      StringBuffer str = new StringBuffer("");
  25.      String st = "";
  26.      try{
  27.       FileInputStream fs = new FileInputStream(filePathAndName);
  28.       InputStreamReader isr;
  29.       if(encoding.equals("")){
  30.        isr = new InputStreamReader(fs);
  31.       }else{
  32.        isr = new InputStreamReader(fs,encoding);
  33.       }
  34.       BufferedReader br = new BufferedReader(isr);
  35.       try{
  36.        String data = "";
  37.        while((data = br.readLine())!=null){
  38.          str.append(data+" "); 
  39.        }
  40.       }catch(Exception e){
  41.        str.append(e.toString());
  42.       }
  43.       st = str.toString();
  44.      }catch(IOException es){
  45.       st = "";
  46.      }
  47.      return st;     
  48.     }
  49.     /**
  50.      * 新建目录
  51.      * @param folderPath 目录
  52.      * @return 返回目录创建后的路径
  53.      */
  54.     public String createFolder(String folderPath) {
  55.         String txt = folderPath;
  56.         try {
  57.             java.io.File myFilePath = new java.io.File(txt);
  58.             txt = folderPath;
  59.             if (!myFilePath.exists()) {
  60.                 myFilePath.mkdir();
  61.             }
  62.         }
  63.         catch (Exception e) {
  64.             message = "创建目录操作出错";
  65.         }
  66.         return txt;
  67.     }
  68.     
  69.     /**
  70.      * 多级目录创建
  71.      * @param folderPath 准备要在本级目录下创建新目录的目录路径 例如 c:myf
  72.      * @param paths 无限级目录参数,各级目录以单数线区分 例如 a|b|c
  73.      * @return 返回创建文件后的路径 例如 c:myfac
  74.      */
  75.     public String createFolders(String folderPath, String paths){
  76.         String txts = folderPath;
  77.         try{
  78.             String txt;
  79.             txts = folderPath;
  80.             StringTokenizer st = new StringTokenizer(paths,"|");
  81.             for(int i=0; st.hasMoreTokens(); i++){
  82.                     txt = st.nextToken().trim();
  83.                     if(txts.lastIndexOf("/")!=-1){ 
  84.                         txts = createFolder(txts+txt);
  85.                     }else{
  86.                         txts = createFolder(txts+txt+"/");    
  87.                     }
  88.             }
  89.        }catch(Exception e){
  90.            message = "创建目录操作出错!";
  91.        }
  92.         return txts;
  93.     }
  94.     
  95.     /**
  96.      * 新建文件
  97.      * @param filePathAndName 文本文件完整绝对路径及文件名
  98.      * @param fileContent 文本文件内容
  99.      * @return
  100.      */
  101.     public void createFile(String filePathAndName, String fileContent) {
  102.      
  103.         try {
  104.             String filePath = filePathAndName;
  105.             filePath = filePath.toString();
  106.             File myFilePath = new File(filePath);
  107.             if (!myFilePath.exists()) {
  108.                 myFilePath.createNewFile();
  109.             }
  110.             FileWriter resultFile = new FileWriter(myFilePath);
  111.             PrintWriter myFile = new PrintWriter(resultFile);
  112.             String strContent = fileContent;
  113.             myFile.println(strContent);
  114.             myFile.close();
  115.             resultFile.close();
  116.         }
  117.         catch (Exception e) {
  118.             message = "创建文件操作出错";
  119.         }
  120.     }
  121.     /**
  122.      * 有编码方式的文件创建
  123.      * @param filePathAndName 文本文件完整绝对路径及文件名
  124.      * @param fileContent 文本文件内容
  125.      * @param encoding 编码方式 例如 GBK 或者 UTF-8
  126.      * @return
  127.      */
  128.     public void createFile(String filePathAndName, String fileContent, String encoding) {
  129.      
  130.         try {
  131.             String filePath = filePathAndName;
  132.             filePath = filePath.toString();
  133.             File myFilePath = new File(filePath);
  134.             if (!myFilePath.exists()) {
  135.                 myFilePath.createNewFile();
  136.             }
  137.             PrintWriter myFile = new PrintWriter(myFilePath,encoding);
  138.             String strContent = fileContent;
  139.             myFile.println(strContent);
  140.             myFile.close();
  141.         }
  142.         catch (Exception e) {
  143.             message = "创建文件操作出错";
  144.         }
  145.     } 
  146.     /**
  147.      * 删除文件
  148.      * @param filePathAndName 文本文件完整绝对路径及文件名
  149.      * @return Boolean 成功删除返回true遭遇异常返回false
  150.      */
  151.     public boolean delFile(String filePathAndName) {
  152.      boolean bea = false;
  153.         try {
  154.             String filePath = filePathAndName;
  155.             File myDelFile = new File(filePath);
  156.             if(myDelFile.exists()){
  157.              myDelFile.delete();
  158.              bea = true;
  159.             }else{
  160.              bea = false;
  161.              message = (filePathAndName+"
  162. 删除文件操作出错");
  163.             }
  164.         }
  165.         catch (Exception e) {
  166.             message = e.toString();
  167.         }
  168.         return bea;
  169.     }
  170.     
  171.     /**
  172.      * 删除文件夹
  173.      * @param folderPath 文件夹完整绝对路径
  174.      * @return
  175.      */
  176.     public void delFolder(String folderPath) {
  177.         try {
  178.             delAllFile(folderPath); //删除完里面所有内容
  179.             String filePath = folderPath;
  180.             filePath = filePath.toString();
  181.             java.io.File myFilePath = new java.io.File(filePath);
  182.             myFilePath.delete(); //删除空文件夹
  183.         }
  184.         catch (Exception e) {
  185.             message = ("删除文件夹操作出错");
  186.         }
  187.     }
  188.     
  189.     
  190.     /**
  191.      * 删除指定文件夹下所有文件
  192.      * @param path 文件夹完整绝对路径
  193.      * @return
  194.      * @return
  195.      */
  196.     public boolean delAllFile(String path) {
  197.      boolean bea = false;
  198.         File file = new File(path);
  199.         if (!file.exists()) {
  200.             return bea;
  201.         }
  202.         if (!file.isDirectory()) {
  203.             return bea;
  204.         }
  205.         String[] tempList = file.list();
  206.         File temp = null;
  207.         for (int i = 0; i < tempList.length; i++) {
  208.             if (path.endsWith(File.separator)) {
  209.                 temp = new File(path + tempList[i]);
  210.             }else{
  211.                 temp = new File(path + File.separator + tempList[i]);
  212.             }
  213.             if (temp.isFile()) {
  214.                 temp.delete();
  215.             }
  216.             if (temp.isDirectory()) {
  217.                 delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
  218.                 delFolder(path+"/"+ tempList[i]);//再删除空文件夹
  219.                 bea = true;
  220.             }
  221.         }
  222.         return bea;
  223.     }
  224.     /**
  225.      * 复制单个文件
  226.      * @param oldPathFile 准备复制的文件源
  227.      * @param newPathFile 拷贝到新绝对路径带文件名
  228.      * @return
  229.      */
  230.     public void copyFile(String oldPathFile, String newPathFile) {
  231.         try {
  232.             int bytesum = 0;
  233.             int byteread = 0;
  234.             File oldfile = new File(oldPathFile);
  235.             if (oldfile.exists()) { //文件存在时
  236.                 InputStream inStream = new FileInputStream(oldPathFile); //读入原文件
  237.                 FileOutputStream fs = new FileOutputStream(newPathFile);
  238.                 byte[] buffer = new byte[1444];
  239.                 while((byteread = inStream.read(buffer)) != -1){
  240.                     bytesum += byteread; //字节数 文件大小
  241.                     System.out.println(bytesum);
  242.                     fs.write(buffer, 0, byteread);
  243.                 }
  244.                 inStream.close();
  245.             }
  246.         }catch (Exception e) {
  247.             message = ("复制单个文件操作出错");
  248.         }
  249.     }
  250.     
  251.     /**
  252.      * 复制整个文件夹的内容
  253.      * @param oldPath 准备拷贝的目录
  254.      * @param newPath 指定绝对路径的新目录
  255.      * @return
  256.      */
  257.     public void copyFolder(String oldPath, String newPath) {
  258.         try {
  259.             new File(newPath).mkdirs(); //如果文件夹不存在 则建立新文件夹
  260.             File a=new File(oldPath);
  261.             String[] file=a.list();
  262.             File temp=null;
  263.             for (int i = 0; i < file.length; i++) {
  264.                 if(oldPath.endsWith(File.separator)){
  265.                     temp=new File(oldPath+file[i]);
  266.                 }else{
  267.                     temp=new File(oldPath+File.separator+file[i]);
  268.                 }
  269.                 if(temp.isFile()){
  270.                     FileInputStream input = new FileInputStream(temp);
  271.                     FileOutputStream output = new FileOutputStream(newPath + "/" +
  272.                     (temp.getName()).toString());
  273.                     byte[] b = new byte[1024 * 5];
  274.                     int len;
  275.                     while ((len = input.read(b)) != -1) {
  276.                         output.write(b, 0, len);
  277.                     }
  278.                     output.flush();
  279.                     output.close();
  280.                     input.close();
  281.                 }
  282.                 if(temp.isDirectory()){//如果是子文件夹
  283.                     copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
  284.                 }
  285.             }
  286.         }catch (Exception e) {
  287.             message = "复制整个文件夹内容操作出错";
  288.         }
  289.     }
  290.     /**
  291.      * 移动文件
  292.      * @param oldPath
  293.      * @param newPath
  294.      * @return
  295.      */
  296.     public void moveFile(String oldPath, String newPath) {
  297.         copyFile(oldPath, newPath);
  298.         delFile(oldPath);
  299.     }
  300.     
  301.     /**
  302.      * 移动目录
  303.      * @param oldPath
  304.      * @param newPath
  305.      * @return
  306.      */
  307.     public void moveFolder(String oldPath, String newPath) {
  308.         copyFolder(oldPath, newPath);
  309.         delFolder(oldPath);
  310.     }
  311.     public String getMessage(){
  312.         return this.message;
  313.     }
  314. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值