`

文件操作

阅读更多

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. }

 

分享到:
评论

相关推荐

    模拟实现采用二级目录结构的磁盘文件系统中的文件操作

    ### 知识点详解 #### 一、二级目录结构及其...通过以上分析可以看出,本实习通过模拟实现采用了二级目录结构的磁盘文件系统中的文件操作,不仅加深了对文件系统原理的理解,还锻炼了数据结构设计和算法实现的能力。

    模拟实现采用二级目录结构的磁盘文件系统中的文件操作。

    模拟实现采用二级目录结构的磁盘文件系统中的文件操作。 文件系统是操作系统中管理和存取信息的机构,它具有“按名存取”的功能,不仅方便用户,而且能提高系统效率且安全可靠。 在用户程序中可使用文件系统提供的...

    CANoe /CAPL 文件操作脚本

    CANoe/CAPL 文件操作脚本是用于自动化处理CANoe环境中的配置、数据记录和分析的编程工具。CANoe是一款广泛应用于汽车电子系统的诊断、测试和测量的软件,而CAPL(CANoe Application Programming Language)是CANoe内...

    21个文件操作VC 源码实例.rar

    收集了21个文件操作VC 源码实例,基础级别的VC 文件操作实例,获得INI文件指定段的全部键名和键值、文件对话框、临时文件创建、目录创建、获得INI文件的全部段名、查找文件、复制文件、获得或设置进程的当前目录、...

    Android JNI调用-文件操作

    在本教程中,我们将重点讨论如何通过JNI在Android应用中进行文件操作。 首先,要使用JNI,我们需要在Java类中声明native方法。例如,我们可以声明一个名为`readFileFromNative`的方法: ```java public class ...

    java文件操作类

    java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...

    操作系统实验磁盘文件操作

    大学本科操作系统实验 《磁盘文件操作模拟C语言》,花了两天的时间调试。

    Unity中Android的文件操作

    原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。

    C++使用hookapi监控文件操作程序

    本项目“C++使用hookapi监控文件操作程序”正是基于这一技术,用于实现对文件系统事件的实时监控。下面将详细介绍相关的知识点。 首先,`hookapi`是指Windows API中的钩子(Hook)机制。钩子是一种让程序能够监视...

    js实现读写文件操作

    js实现的读写文件,文件放在的c:\12.txt里

    C#编程 文件操作 ReadFileByLine(源码)(源码)

    C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine...

    C#编程 文件操作 ManageFileByType(源码)(源码)

    C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ...

    C#编程 文件操作 DeleteDirByDG(源码)(源码)

    C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)...

    C#编程 文件操作 OperateXML(源码)(源码)

    C#编程 文件操作 OperateXML(源码)(源码)C#编程 文件操作 OperateXML(源码)(源码)C#编程 文件操作 OperateXML(源码)(源码)C#编程 文件操作 OperateXML(源码)(源码)C#编程 文件操作 OperateXML(源码)(源码)C#编程 ...

    操作系统实验4_文件系统

    操作系统实验四的核心目标是设计和实现一个简单的...通过这样的实验,学生能够深入理解文件系统如何管理磁盘空间,跟踪文件元数据,以及如何执行基本的文件操作,这对理解和设计更复杂的操作系统有着至关重要的作用。

    C#文件操作大全.pdf

    根据提供的信息,我们可以总结出以下关于C#文件操作的关键知识点: ### 1. 创建文件夹 在C#中,可以通过`System.IO`命名空间中的`Directory.CreateDirectory`方法来创建一个新的文件夹。 ```csharp using System....

    pb操作TXT文件(打开,写入,关闭,删除等等)

    - **错误处理**:在进行文件操作时,应添加适当的错误处理代码,如`Try...Catch`结构,以应对可能出现的异常情况,如文件不存在、权限问题等。 - **定位**:可以使用`Seek`方法改变读写位置,`Tell`方法获取当前...

    VC之PDF文件操作

    在VC++环境中进行PDF文件操作是一项常见的任务,尤其在开发桌面应用程序时,可能需要读取、编辑或生成PDF文档。本篇文章将详细讲解如何在VC++中实现这些功能,主要涉及的技术点包括PDF文件的基本概念、PDF库的使用...

    C#窗体文件操作案例 c#经典案例.doc

    C# 文件操作案例分析 本文将对 C# 窗体文件操作案例进行详细分析,涵盖文件操作的基本概念、文件流操作、文本和二进制文件读写、文件属性读取等知识点。 文件操作基本概念 文件操作是计算机编程中最基本的操作之...

    linux C语言 使用结构体对文件操作 读 写 查找

    在Linux系统中,C语言是进行系统级编程的首选语言,尤其在文件操作方面,它提供了丰富的函数库和系统调用来处理各种文件操作任务。本教程将深入探讨如何使用C语言中的结构体来管理和操作文件。 首先,让我们理解...

Global site tag (gtag.js) - Google Analytics