一个写文件的java类

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值