JAVA基础学习篇----FILE OPERATOR

JAVA文件操作相关:
  1. package com.bytecode.openexcel.util;
  2. import java.io.*;
  3. public class FileOperate {
  4.     
  5.     public FileOperate() {
  6.     }
  7.     /**
  8.      * 新建目录
  9.      * 
  10.      * @param folderPath
  11.      *            String 如 c:/fqf
  12.      * @return boolean
  13.      */
  14.     public void newFolder(String folderPath) {
  15.         try {
  16.             String filePath = folderPath;
  17.             filePath = filePath.toString();
  18.             java.io.File myFilePath = new java.io.File(filePath);
  19.             if (!myFilePath.exists()) {
  20.                 myFilePath.mkdir();
  21.             }
  22.         } catch (Exception e) {
  23.             System.out.println("新建目录操作出错 ");
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.     /**
  28.      * 新建文件
  29.      * 
  30.      * @param filePathAndName
  31.      *            String 文件路径及名称 如c:/fqf.txt
  32.      * @param fileContent
  33.      *            String 文件内容
  34.      * @return boolean
  35.      */
  36.     public void newFile(String filePathAndName, String fileContent) {
  37.         try {
  38.             String filePath = filePathAndName;
  39.             filePath = filePath.toString();
  40.             File myFilePath = new File(filePath);
  41.             if (!myFilePath.exists()) {
  42.                 myFilePath.createNewFile();
  43.             }
  44.             FileWriter resultFile = new FileWriter(myFilePath);
  45.             PrintWriter myFile = new PrintWriter(resultFile);
  46.             String strContent = fileContent;
  47.             myFile.println(strContent);
  48.             resultFile.close();
  49.         } catch (Exception e) {
  50.             System.out.println("新建文件操作出错 ");
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.     /**
  55.      * 删除文件
  56.      * 
  57.      * @param filePathAndName
  58.      *            String 文件路径及名称 如c:/fqf.txt
  59.      * @param fileContent
  60.      *            String
  61.      * @return boolean
  62.      */
  63.     public void delFile(String filePathAndName) {
  64.         try {
  65.             String filePath = filePathAndName;
  66.             filePath = filePath.toString();
  67.             java.io.File myDelFile = new java.io.File(filePath);
  68.             myDelFile.delete();
  69.         } catch (Exception e) {
  70.             System.out.println("删除文件操作出错 ");
  71.             e.printStackTrace();
  72.         }
  73.     }
  74.     /**
  75.      * 删除文件夹
  76.      * 
  77.      * @param filePathAndName
  78.      *            String 文件夹路径及名称 如c:/fqf
  79.      * @param fileContent
  80.      *            String
  81.      * @return boolean
  82.      */
  83.     public void delFolder(String folderPath) {
  84.         try {
  85.             delAllFile(folderPath); // 删除完里面所有内容
  86.             String filePath = folderPath;
  87.             filePath = filePath.toString();
  88.             java.io.File myFilePath = new java.io.File(filePath);
  89.             myFilePath.delete(); // 删除空文件夹
  90.         } catch (Exception e) {
  91.             System.out.println("删除文件夹操作出错 ");
  92.             e.printStackTrace();
  93.         }
  94.     }
  95.     /**
  96.      * 删除文件夹里面的所有文件
  97.      * 
  98.      * @param path
  99.      *            String 文件夹路径 如 c:/fqf
  100.      */
  101.     public void delAllFile(String path) {
  102.         File file = new File(path);
  103.         if (!file.exists()) {
  104.             return;
  105.         }
  106.         if (!file.isDirectory()) {
  107.             return;
  108.         }
  109.         String[] tempList = file.list();
  110.         File temp = null;
  111.         for (int i = 0; i < tempList.length; i++) {
  112.             if (path.endsWith(File.separator)) {
  113.                 temp = new File(path + tempList[i]);
  114.             } else {
  115.                 temp = new File(path + File.separator + tempList[i]);
  116.             }
  117.             if (temp.isFile()) {
  118.                 temp.delete();
  119.             }
  120.             if (temp.isDirectory()) {
  121.                 delAllFile(path + "/ " + tempList[i]);// 先删除文件夹里面的文件
  122.                 delFolder(path + "/ " + tempList[i]);// 再删除空文件夹
  123.             }
  124.         }
  125.     }
  126.     /**
  127.      * 复制单个文件
  128.      * 
  129.      * @param oldPath
  130.      *            String 原文件路径 如:c:/fqf.txt
  131.      * @param newPath
  132.      *            String 复制后路径 如:f:/fqf.txt
  133.      * @return boolean
  134.      */
  135.     public void copyFile(String oldPath, String newPath) {
  136.         try {
  137.             int bytesum = 0;
  138.             int byteread = 0;
  139.             File oldfile = new File(oldPath);
  140.             if (oldfile.exists()) { // 文件存在时
  141.                 InputStream inStream = new FileInputStream(oldPath); // 读入原文件
  142.                 FileOutputStream fs = new FileOutputStream(newPath);
  143.                 byte[] buffer = new byte[1444];
  144.                 int length;
  145.                 while ((byteread = inStream.read(buffer)) != -1) {
  146.                     bytesum += byteread; // 字节数 文件大小
  147.                     System.out.println(bytesum);
  148.                     fs.write(buffer, 0, byteread);
  149.                 }
  150.                 inStream.close();
  151.             }
  152.         } catch (Exception e) {
  153.             System.out.println("复制单个文件操作出错 ");
  154.             e.printStackTrace();
  155.         }
  156.     }
  157.     /**
  158.      * 复制整个文件夹内容
  159.      * 
  160.      * @param oldPath
  161.      *            String 原文件路径 如:c:/fqf
  162.      * @param newPath
  163.      *            String 复制后路径 如:f:/fqf/ff
  164.      * @return boolean
  165.      */
  166.     public void copyFolder(String oldPath, String newPath) {
  167.         try {
  168.             (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
  169.             File a = new File(oldPath);
  170.             String[] file = a.list();
  171.             File temp = null;
  172.             for (int i = 0; i < file.length; i++) {
  173.                 if (oldPath.endsWith(File.separator)) {
  174.                     temp = new File(oldPath + file[i]);
  175.                 } else {
  176.                     temp = new File(oldPath + File.separator + file[i]);
  177.                 }
  178.                 if (temp.isFile()) {
  179.                     FileInputStream input = new FileInputStream(temp);
  180.                     FileOutputStream output = new FileOutputStream(newPath
  181.                             + "/ " + (temp.getName()).toString());
  182.                     byte[] b = new byte[1024 * 5];
  183.                     int len;
  184.                     while ((len = input.read(b)) != -1) {
  185.                         output.write(b, 0, len);
  186.                     }
  187.                     output.flush();
  188.                     output.close();
  189.                     input.close();
  190.                 }
  191.                 if (temp.isDirectory()) {// 如果是子文件夹
  192.                     copyFolder(oldPath + "/ " + file[i], newPath + "/ "
  193.                             + file[i]);
  194.                 }
  195.             }
  196.         } catch (Exception e) {
  197.             System.out.println("复制整个文件夹内容操作出错 ");
  198.             e.printStackTrace();
  199.         }
  200.     }
  201.     /**
  202.      * 移动文件到指定目录
  203.      * 
  204.      * @param oldPath
  205.      *            String 如:c:/fqf.txt
  206.      * @param newPath
  207.      *            String 如:d:/fqf.txt
  208.      */
  209.     public void moveFile(String oldPath, String newPath) {
  210.         copyFile(oldPath, newPath);
  211.         delFile(oldPath);
  212.     }
  213.     /**
  214.      * 移动文件到指定目录
  215.      * 
  216.      * @param oldPath
  217.      *            String 如:c:/fqf.txt
  218.      * @param newPath
  219.      *            String 如:d:/fqf.txt
  220.      */
  221.     public void moveFolder(String oldPath, String newPath) {
  222.         copyFolder(oldPath, newPath);
  223.         delFolder(oldPath);
  224.     }
  225. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值