码迷,mamicode.com
首页 > 编程语言 > 详细

Java IO(文件操作工具类)

时间:2014-05-19 23:45:36      阅读:530      评论:0      收藏:0      [点我收藏+]

标签:blog   class   c   java   tar   ext   

FileOperate实现的功能:

 

    1. 返回文件夹中所有文件列表

    2. 读取文本文件内容

    3. 新建目录

    4. 新建多级目录

    5. 新建文件

    6. 有编码方式的创建文件

    7. 删除文件

    8. 删除指定文件夹下所有文件

    9. 复制单个文件

    10. 复制整个文件夹的内容

    11. 移动文件

    12. 移动目录

    13. 建立一个可以追加的bufferedwriter

    14. 得到一个bufferedreader

Java代码   bubuko.com,布布扣
  1. package utils;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileOutputStream;  
  9. import java.io.FileWriter;  
  10. import java.io.IOException;  
  11. import java.io.InputStream;  
  12. import java.io.InputStreamReader;  
  13. import java.io.PrintWriter;  
  14. import java.util.StringTokenizer;  
  15.   
  16. public class FileOperate {  
  17.   
  18.     private String message;  
  19.   
  20.     public FileOperate() {  
  21.     }  
  22.   
  23.     /** 
  24.      * 遍历文件夹中文件 
  25.      *  
  26.      * @param filepath 
  27.      * @return 返回file[]数组 
  28.      */  
  29.     public File[] getFileList(String filepath) {  
  30.         File d = null;  
  31.         File list[] = null;  
  32.         // 建立当前目录中文件的File对象  
  33.         try {  
  34.             d = new File(filepath);  
  35.             if (d.exists()) {  
  36.                 list = d.listFiles();  
  37.             }  
  38.         } catch (Exception ex) {  
  39.             ex.printStackTrace();  
  40.             message = "遍历文件夹出错";  
  41.         }  
  42.         // 取得代表目录中所有文件的File对象数组  
  43.   
  44.         return list;  
  45.     }  
  46.   
  47.     /** 
  48.      * 读取文本文件内容 
  49.      *  
  50.      * @param filePathAndName 
  51.      *            带有完整绝对路径的文件名 
  52.      * @param encoding 
  53.      *            文本文件打开的编码方式 
  54.      * @return 返回文本文件的内容 
  55.      */  
  56.     public String readTxt(String filePathAndName, String encoding)  
  57.             throws IOException {  
  58.         encoding = encoding.trim();  
  59.         StringBuffer str = new StringBuffer("");  
  60.         String st = "";  
  61.         try {  
  62.             FileInputStream fs = new FileInputStream(filePathAndName);  
  63.             InputStreamReader isr;  
  64.             if (encoding.equals("")) {  
  65.                 isr = new InputStreamReader(fs);  
  66.             } else {  
  67.                 isr = new InputStreamReader(fs, encoding);  
  68.             }  
  69.             BufferedReader br = new BufferedReader(isr);  
  70.             try {  
  71.                 String data = "";  
  72.                 while ((data = br.readLine()) != null) {  
  73.                     str.append(data);  
  74.                 }  
  75.             } catch (Exception e) {  
  76.                 str.append(e.toString());  
  77.             }  
  78.             st = str.toString();  
  79.             if (st != null && st.length() > 1)  
  80.                 st = st.substring(0, st.length() - 1);  
  81.         } catch (IOException es) {  
  82.             st = "";  
  83.         }  
  84.         return st;  
  85.     }  
  86.   
  87.     /** 
  88.      * 新建目录 
  89.      *  
  90.      * @param folderPath 
  91.      *            目录 
  92.      * @return 返回目录创建后的路径 
  93.      */  
  94.     public String createFolder(String folderPath) {  
  95.         String txt = folderPath;  
  96.         try {  
  97.             java.io.File myFilePath = new java.io.File(txt);  
  98.             txt = folderPath;  
  99.             if (!myFilePath.exists()) {  
  100.                 myFilePath.mkdir();  
  101.             }  
  102.         } catch (Exception e) {  
  103.             message = "创建目录操作出错";  
  104.         }  
  105.         return txt;  
  106.     }  
  107.   
  108.     /** 
  109.      * 多级目录创建 
  110.      *  
  111.      * @param folderPath 
  112.      *            准备要在本级目录下创建新目录的目录路径例如 c:myf 
  113.      * @param paths 
  114.      *            无限级目录参数,各级目录以单数线区分 例如 a|b|c 
  115.      * @return 返回创建文件后的路径 
  116.      */  
  117.     public String createFolders(String folderPath, String paths) {  
  118.         String txts = folderPath;  
  119.         try {  
  120.             String txt;  
  121.             txts = folderPath;  
  122.             StringTokenizer st = new StringTokenizer(paths, "|");  
  123.             for (int i = 0; st.hasMoreTokens(); i++) {  
  124.                 txt = st.nextToken().trim();  
  125.                 if (txts.lastIndexOf("/") != -1) {  
  126.                     txts = createFolder(txts + txt);  
  127.                 } else {  
  128.                     txts = createFolder(txts + txt + "/");  
  129.                 }  
  130.             }  
  131.         } catch (Exception e) {  
  132.             message = "创建目录操作出错";  
  133.         }  
  134.         return txts;  
  135.     }  
  136.   
  137.     /** 
  138.      * 新建文件 
  139.      *  
  140.      * @param filePathAndName 
  141.      *            文本文件完整绝对路径及文件名 
  142.      * @param fileContent 
  143.      *            文本文件内容 
  144.      * @return 
  145.      */  
  146.     public void createFile(String filePathAndName, String fileContent) {  
  147.   
  148.         try {  
  149.             String filePath = filePathAndName;  
  150.             filePath = filePath.toString();  
  151.             File myFilePath = new File(filePath);  
  152.             if (!myFilePath.exists()) {  
  153.                 myFilePath.createNewFile();  
  154.             }  
  155.             FileWriter resultFile = new FileWriter(myFilePath);  
  156.             PrintWriter myFile = new PrintWriter(resultFile);  
  157.             String strContent = fileContent;  
  158.             myFile.println(strContent);  
  159.             myFile.close();  
  160.             resultFile.close();  
  161.         } catch (Exception e) {  
  162.             message = "创建文件操作出错";  
  163.         }  
  164.     }  
  165.   
  166.     /** 
  167.      * 有编码方式的文件创建 
  168.      *  
  169.      * @param filePathAndName 
  170.      *            文本文件完整绝对路径及文件名 
  171.      * @param fileContent 
  172.      *            文本文件内容 
  173.      * @param encoding 
  174.      *            编码方式 例如 GBK 或者 UTF-8 
  175.      * @return 
  176.      */  
  177.     public void createFile(String filePathAndName, String fileContent,  
  178.             String encoding) {  
  179.   
  180.         try {  
  181.             String filePath = filePathAndName;  
  182.             filePath = filePath.toString();  
  183.             File myFilePath = new File(filePath);  
  184.             if (!myFilePath.exists()) {  
  185.                 myFilePath.createNewFile();  
  186.             }  
  187.             PrintWriter myFile = new PrintWriter(myFilePath, encoding);  
  188.             String strContent = fileContent;  
  189.             myFile.println(strContent);  
  190.             myFile.close();  
  191.         } catch (Exception e) {  
  192.             message = "创建文件操作出错";  
  193.         }  
  194.     }  
  195.   
  196.     /** 
  197.      * 删除文件 
  198.      *  
  199.      * @param filePathAndName 
  200.      *            文本文件完整绝对路径及文件名 
  201.      * @return Boolean 成功删除返回true遭遇异常返回false 
  202.      */  
  203.     public boolean delFile(String filePathAndName) {  
  204.         boolean bea = false;  
  205.         try {  
  206.             String filePath = filePathAndName;  
  207.             File myDelFile = new File(filePath);  
  208.             if (myDelFile.exists()) {  
  209.                 myDelFile.delete();  
  210.                 bea = true;  
  211.             } else {  
  212.                 bea = false;  
  213.                 message = (filePathAndName + "删除文件操作出错");  
  214.             }  
  215.         } catch (Exception e) {  
  216.             message = e.toString();  
  217.         }  
  218.         return bea;  
  219.     }  
  220.   
  221.     /** 
  222.      * 删除文件 
  223.      *  
  224.      * @param folderPath 
  225.      *            文件夹完整绝对路径 
  226.      * @return 
  227.      */  
  228.     public void delFolder(String folderPath) {  
  229.         try {  
  230.             delAllFile(folderPath); // 删除完里面所有内容  
  231.             String filePath = folderPath;  
  232.             filePath = filePath.toString();  
  233.             java.io.File myFilePath = new java.io.File(filePath);  
  234.             myFilePath.delete(); // 删除空文件夹  
  235.         } catch (Exception e) {  
  236.             message = ("删除文件夹操作出错");  
  237.         }  
  238.     }  
  239.   
  240.     /** 
  241.      * 删除指定文件夹下所有文件 
  242.      *  
  243.      * @param path 
  244.      *            文件夹完整绝对路径 
  245.      * @return 
  246.      * @return 
  247.      */  
  248.     public boolean delAllFile(String path) {  
  249.         boolean bea = false;  
  250.         File file = new File(path);  
  251.         if (!file.exists()) {  
  252.             return bea;  
  253.         }  
  254.         if (!file.isDirectory()) {  
  255.             return bea;  
  256.         }  
  257.         String[] tempList = file.list();  
  258.         File temp = null;  
  259.         for (int i = 0; i < tempList.length; i++) {  
  260.             if (path.endsWith(File.separator)) {  
  261.                 temp = new File(path + tempList[i]);  
  262.             } else {  
  263.                 temp = new File(path + File.separator + tempList[i]);  
  264.             }  
  265.             if (temp.isFile()) {  
  266.                 temp.delete();  
  267.             }  
  268.             if (temp.isDirectory()) {  
  269.                 delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件  
  270.                 delFolder(path + "/" + tempList[i]);// 再删除空文件  
  271.                 bea = true;  
  272.             }  
  273.         }  
  274.         return bea;  
  275.     }  
  276.   
  277.     /** 
  278.      * 复制单个文件 
  279.      *  
  280.      * @param oldPathFile 
  281.      *            准备复制的文件源 
  282.      * @param newPathFile 
  283.      *            拷贝到新绝对路径带文件名 
  284.      * @return 
  285.      */  
  286.     public void copyFile(String oldPathFile, String newPathFile) {  
  287.         try {  
  288.             int bytesum = 0;  
  289.             int byteread = 0;  
  290.             File oldfile = new File(oldPathFile);  
  291.             if (oldfile.exists()) { // 文件存在  
  292.                 InputStream inStream = new FileInputStream(oldPathFile); // 读入源文件  
  293.                 FileOutputStream fs = new FileOutputStream(newPathFile);  
  294.                 byte[] buffer = new byte[1444];  
  295.                 while ((byteread = inStream.read(buffer)) != -1) {  
  296.                     bytesum += byteread; // 字节 文件大小  
  297.                     System.out.println(bytesum);  
  298.                     fs.write(buffer, 0, byteread);  
  299.                 }  
  300.                 inStream.close();  
  301.             }  
  302.         } catch (Exception e) {  
  303.             message = ("复制单个文件操作出错");  
  304.         }  
  305.     }  
  306.   
  307.     /** 
  308.      * 复制整个文件夹的内容 
  309.      *  
  310.      * @param oldPath 
  311.      *            准备拷贝的目录 
  312.      * @param newPath 
  313.      *            指定绝对路径的新目录 
  314.      * @return 
  315.      */  
  316.     public void copyFolder(String oldPath, String newPath) {  
  317.         try {  
  318.             new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件  
  319.             File a = new File(oldPath);  
  320.             String[] file = a.list();  
  321.             File temp = null;  
  322.             for (int i = 0; i < file.length; i++) {  
  323.                 if (oldPath.endsWith(File.separator)) {  
  324.                     temp = new File(oldPath + file[i]);  
  325.                 } else {  
  326.                     temp = new File(oldPath + File.separator + file[i]);  
  327.                 }  
  328.                 if (temp.isFile()) {  
  329.                     FileInputStream input = new FileInputStream(temp);  
  330.                     FileOutputStream output = new FileOutputStream(newPath  
  331.                             + "/" + (temp.getName()).toString());  
  332.                     byte[] b = new byte[1024 * 5];  
  333.                     int len;  
  334.                     while ((len = input.read(b)) != -1) {  
  335.                         output.write(b, 0, len);  
  336.                     }  
  337.                     output.flush();  
  338.                     output.close();  
  339.                     input.close();  
  340.                 }  
  341.                 if (temp.isDirectory()) {// 如果是子文件  
  342.                     copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);  
  343.                 }  
  344.             }  
  345.         } catch (Exception e) {  
  346.             message = "复制整个文件夹内容操作出错";  
  347.         }  
  348.     }  
  349.   
  350.     /** 
  351.      * 移动文件 
  352.      *  
  353.      * @param oldPath 
  354.      * @param newPath 
  355.      * @return 
  356.      */  
  357.     public void moveFile(String oldPath, String newPath) {  
  358.         copyFile(oldPath, newPath);  
  359.         delFile(oldPath);  
  360.     }  
  361.   
  362.     /** 
  363.      * 移动目录 
  364.      *  
  365.      * @param oldPath 
  366.      * @param newPath 
  367.      * @return 
  368.      */  
  369.     public void moveFolder(String oldPath, String newPath) {  
  370.         copyFolder(oldPath, newPath);  
  371.         delFolder(oldPath);  
  372.     }  
  373.   
  374.     /** 
  375.      * 建立一个可以追加的bufferedwriter 
  376.      *  
  377.      * @param fileDir 
  378.      * @param fileName 
  379.      * @return 
  380.      */  
  381.     public BufferedWriter getWriter(String fileDir, String fileName) {  
  382.         try {  
  383.             File f1 = new File(fileDir);  
  384.             if (!f1.exists()) {  
  385.                 f1.mkdirs();  
  386.             }  
  387.             f1 = new File(fileDir, fileName);  
  388.             if (!f1.exists()) {  
  389.                 f1.createNewFile();  
  390.             }  
  391.             BufferedWriter bw = new BufferedWriter(new FileWriter(f1.getPath(),  
  392.                     true));  
  393.             return bw;  
  394.         } catch (Exception e) {  
  395.             System.out.println(e.getLocalizedMessage());  
  396.             return null;  
  397.         }  
  398.     }  
  399.   
  400.     /** 
  401.      * 得到一个bufferedreader 
  402.      *  
  403.      * @param fileDir 
  404.      * @param fileName 
  405.      * @param encoding 
  406.      * @return 
  407.      */  
  408.     public BufferedReader getReader(String fileDir, String fileName,  
  409.             String encoding) {  
  410.         try {  
  411.             File file = new File(fileDir, fileName);  
  412.             InputStreamReader read = new InputStreamReader(new FileInputStream(  
  413.                     file), encoding);  
  414.             BufferedReader br = new BufferedReader(read);  
  415.             return br;  
  416.   
  417.         } catch (FileNotFoundException ex) {  
  418.             ex.printStackTrace();  
  419.             return null;  
  420.         } catch (IOException e) {  
  421.             e.printStackTrace();  
  422.             return null;  
  423.         }  
  424.   
  425.     }  
  426.   
  427.     public String getMessage() {  
  428.         return this.message;  
  429.     }  
  430. }  

Java IO(文件操作工具类),布布扣,bubuko.com

Java IO(文件操作工具类)

标签:blog   class   c   java   tar   ext   

原文地址:http://www.cnblogs.com/564085446java/p/3731252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!