package cn.edu.tongji.cims.wade.system;
import java.io.*;
public class FileOperate {
public FileOperate() {
}
/**
* 新建目录
* @param folderPath String 如 c:/fqf
* @return boolean
*/
public void newFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}
/**
* 新建文件
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt
* @param fileContent String 文件内容
* @return boolean
*/
public void newFile(String filePathAndName, String fileContent) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
resultFile.close();
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}
/**
* 删除文件
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt
* @param fileContent String
* @return boolean
*/
public void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
}
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
/**
* 删除文件夹
* @param filePathAndName String 文件夹路径及名称 如c:/fqf
* @param fileContent String
* @return boolean
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
/**
* 删除文件夹里面的所有文件
* @param path String 文件夹路径 如 c:/fqf
*/
public void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
}
else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
delFolder(path+"/"+ tempList[i]);//再删除空文件夹
}
}
}
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public void copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
分享到:
相关推荐
Java.io 包中的 File 类 Java.io 包中的 File 类是 Java 语言中代表磁盘文件本身的对象,定义了一些与平台无关的方法来...Java.io 包中的 File 类提供了许多实用的方法来操纵文件,帮助开发者更方便地处理文件操作。
在Java编程中,"java.io.FileNotFoundException: ***** (Too many open files)" 是一个常见的错误,意味着程序尝试打开的文件数量超过了操作系统的限制。这个错误通常出现在处理大量文件或长时间运行的程序中,尤其...
在Java中,`java.io.File`类是用于操作文件和目录的基本工具。它可以用来创建、删除、重命名文件,以及获取文件的属性信息,如大小、路径、最后修改时间等。 要读取一个TXT文档,我们需要使用Java的I/O流。这里主要...
本资源“java_io.rar”提供了关于如何在Java中进行文件操作的示例代码,包括读取、移动、删除和复制文件等常见任务。我们将深入探讨这些主题,以便更好地理解Java I/O API的使用。 首先,让我们从读取本地文件开始...
java.io clojure.java.io 的 JK7 java.nio.file.Path 兼容性依赖信息该库托管在 Releases 上。 依赖: [me.moocar/java.io " 0.1.0 " ]用法是 JDK7 中引入的文件路径的抽象。 这个库提供了和 Paths 之间的兼容性。 ...
`File`类帮助编写跨平台的代码,用于检查和操作文件和目录。 ### Java.nio简介 Java.nio(New I/O)是Java 1.4版本引入的一个新框架,旨在提供更高效的I/O操作,特别是在大数据量处理和网络通信方面。它引入了基于...
首先,Java中的文件IO操作主要依赖于`java.io`包中的类,如`File`、`FileReader`、`FileWriter`、`BufferedReader`和`BufferedWriter`等。在描述中提到的`File`类是Java中的核心类,用于表示文件和目录路径名的抽象...
在Java中,我们可以使用`java.io.File`类来创建、删除、重命名或检查文件是否存在。例如,创建一个新文件的代码如下: ```java File file = new File("newFile.txt"); file.createNewFile(); ``` 如果要移动文件,...
在Java中,IO操作主要分为字节流和字符流两大类,分别由`java.io.InputStream`和`java.io.OutputStream`以及`java.io.Reader`和`java.io.Writer`作为基类。字节流处理的是8位的字节,而字符流处理的是16位的Unicode...
3. **BufferedReader和BufferedWriter**:在Java中,`java.io.BufferedReader`和`java.io.BufferedWriter`类用于读写文件。这两个类允许我们指定文件的字符编码,通过构造函数传入`Charset`对象。 4. **文件读写...
这个包包含了大量类和接口,它们提供了各种输入输出流、字符编码、数据转换以及文件操作等功能。Java IO的设计采用了Decorator模式,使得在不修改原有类的基础上,能够动态地增加新功能。 Decorator模式是一种设计...
在Java开发过程中,使用文件I/O操作时遇到java.io.FileNotFoundException异常是一个比较常见的问题。即使文件路径正确无误,也可能会因为多种原因导致这个异常的发生。该异常属于编译时异常,即必须显式处理才能通过...
6. **java.io.File**:文件操作类,可以创建、删除、重命名文件,获取文件属性等。 7. **java.nio.file.***(Java 7及以上):非阻塞I/O框架,包括`Path`、`Files`、`FileSystem`等,提供了更高效的文件操作。 8. ...
`java.nio.file.Files`是一个实用工具类,提供大量静态方法来执行各种文件系统操作。例如: ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file....
在Java编程语言中,`java.io.File`类是处理文件和目录的核心类。...在实际项目中,结合`File`类和其他IO流类,开发者可以实现各种复杂的文件操作。对于初学者来说,理解和掌握`File`类的使用是学习Java IO的基础。
### Java IO操作详解 ...通过以上介绍,我们详细了解了Java IO操作中的一些基本概念和技术点,包括File类的使用、基本的输入输出流操作以及RandomAccessFile的使用等。这些知识对于进行文件读写操作至关重要。
Apache Commons IO库中的`org.apache.commons.io.FileUtils`类是一个非常实用的工具类,它提供了大量方便、高效的方法,用于处理文件系统操作。这个类在Java开发中被广泛使用,尤其是在处理大量的文件读写和管理任务...
前言 其实在网上有很多介绍下载文件或者解压zip文件的文章,但是...import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp