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