`

文件操作

 
阅读更多
package file;

import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/*
 * 操作文件,移动,删除,拷贝
 */
public class OperateFile {
	public static boolean renameFile(String strSrcPath, String strDstPath) {
		String srcPath, dstPath;
		srcPath = strSrcPath.trim();
		dstPath = strDstPath.trim();
		if (srcPath.charAt(srcPath.length() - 1) == File.separatorChar) // 为/或\\
			srcPath = srcPath.substring(0, srcPath.length() - 1);
		if (dstPath.charAt(dstPath.length() - 1) == File.separatorChar)
			dstPath = dstPath.substring(0, dstPath.length() - 1);
		if (srcPath.equals("") || dstPath.equals("") || srcPath.equals(dstPath)) {
			System.out.println("Path is error(FileOperate.renameFile())");
			return false;
		}
		File file1 = new File(srcPath);
		if (!file1.exists()) {
			System.out
					.println("Source Path is error does't exist!(FileOperate.renameFile())");
			return false;
		}
		if (file1.isDirectory())
		// dir
		{
			System.out
					.println("Source Path isn't file(FileOperate.renameFile())");
			return false;
		} else // file
		{
			File file2 = new File(dstPath);
			if (file2.exists()) {
				if (file2.isDirectory()) {
					System.out
							.println("Destination Path is error duplicate file_name!(FileOperate.copyFile())");
					return false;
				} else {
					file2.delete();
				}
			} else {   //file2不存在
				File file3 = file2.getParentFile();
				if ((file3 != null) && !file3.exists()) {
					file3.mkdirs();
				}
			}
			return file1.renameTo(file2);
		}
	}// function renameFile end

	public static void copyFile(String strSrcPath, String strDstPath) {
		String srcPath, dstPath;
		srcPath = strSrcPath.trim();
		dstPath = strDstPath.trim();
		if (srcPath.charAt(srcPath.length() - 1) == File.separatorChar)
			srcPath = srcPath.substring(0, srcPath.length() - 1);
		if (dstPath.charAt(dstPath.length() - 1) == File.separatorChar)
			dstPath = dstPath.substring(0, dstPath.length() - 1);
		if (srcPath.equals("") || dstPath.equals("") || srcPath.equals(dstPath)) {
			System.out.println("Path is error(FileOperate.copyFile())");
			return;
		}
		File file1 = new File(srcPath);
		if (!file1.exists() || file1.isDirectory()) {
			System.out.println("Source Path is error(FileOperate.copyFile())");
			return;
		}
		File file2 = new File(dstPath);
		File file3 = file2.getParentFile();
		if ((file3 != null) && !file3.exists()) {
			file3.mkdirs();
		}
		try {
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(srcPath), 8192);
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(dstPath), 8192);
			byte[] btData = new byte[1024];
			int size;
			while ((size = in.read(btData)) != -1) {
				out.write(btData, 0, size);
			}
			out.flush();
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}// copyFile end

	public static void copyFiles(String strSrcDir, String strDstDir) {
		String srcDir, dstDir;
		srcDir = strSrcDir.trim();
		dstDir = strDstDir.trim();
		if (!srcDir.equals(File.separator))
			if (srcDir.charAt(srcDir.length() - 1) == File.separatorChar)
				srcDir = srcDir.substring(0, srcDir.length() - 1);
		if (!dstDir.equals(File.separator))
			if (dstDir.charAt(dstDir.length() - 1) == File.separatorChar)
				dstDir = dstDir.substring(0, dstDir.length() - 1);
		if (srcDir.equals("") || dstDir.equals("") || srcDir.equals(dstDir)) {
			System.out.println("Path is error(FileOperate.copyFiles())");
			return;
		}
		File file1 = new File(srcDir);
		if (!file1.isDirectory()) {
			System.out.println("Source Path is error(FileOperate.copyFiles())");
			return;
		}
		File file2 = new File(dstDir);
		if (file2.exists()) {
			if (!file2.isDirectory()) {
				System.out
						.println("Destination Path is error(FileOperate.copyFiles())");
				return;
			}
		} else {
			file2.mkdirs();
		}
		String[] fileNames = file1.list();
		for (int i = 0; i < fileNames.length; i++) {
			File file = new File(file1, fileNames[i]);
			if (file.isDirectory()) {
				copyFiles(srcDir + File.separator + fileNames[i], dstDir
						+ File.separator + fileNames[i]);
			} else {
				copyFile(srcDir + File.separator + fileNames[i], dstDir
						+ File.separator + fileNames[i]);
			}
		}
	}// copyFiles end

	public static void moveFile(String strSrcPath, String strDstPath) {
		copyFile(strSrcPath, strDstPath);
		deleteFile(strSrcPath);
	}// moveFile end

	public static void moveFiles(String strSrcDir, String strDstDir) {
		copyFiles(strSrcDir, strDstDir);
		deleteFiles(strSrcDir, true);
	}// moveFiles end

	public static boolean deleteFile(String strPath) {
		File file = new File(strPath.trim());
		if (file.exists()) {
			System.out
					.println("delete file is unsuccessful(FileOperate.deleteFile())");
			return file.delete();
		} else {
			System.out
					.println("File or Dir isn't existent(FileOperate.deleteFile())");
			return false;
		}
	}// deleteFile end

	public static void deleteFiles(String strDir, boolean deleteDir) {
		String dir = strDir.trim();
		if (dir.charAt(dir.length() - 1) == File.separatorChar)
			dir = dir.substring(0, dir.length() - 1);
		if (dir.equals("")) {
			System.out.println("Path is error(FileOperate.deleteFiles())");
			return;
		}
		File file = new File(dir);
		if (!file.exists()) {
			System.out
					.println("file or dir isn't existent(FileOperate.deleteFiles())");
			return;
		}
		if (file.isDirectory()) {
			String[] fileNames = file.list();
			for (int i = 0; i < fileNames.length; i++) {
				File subFile = new File(file, fileNames[i]);
				if (subFile.isDirectory()) {
					deleteFiles(dir + File.separator + fileNames[i], deleteDir);
					if (deleteDir)
						subFile.delete();
				} else {
					subFile.delete();
				}
			}
			if (deleteDir)
				file.delete();
		} else {
			file.delete();
		}
	}

	// deleteFiles end
	public static void zip(String strSrcPath, String strDstPath) {
		String srcPath, dstPath;
		srcPath = strSrcPath.trim();
		dstPath = strDstPath.trim();
		if (srcPath.charAt(srcPath.length() - 1) == File.separatorChar)
			srcPath = srcPath.substring(0, srcPath.length() - 1);
		if (dstPath.charAt(dstPath.length() - 1) == File.separatorChar)
			dstPath = dstPath.substring(0, dstPath.length() - 1);
		if (srcPath.equals("") || dstPath.equals("") || srcPath.equals(dstPath)) {
			System.out.println("Path is error(FileOperate.zip())");
			return;
		}
		File srcFile = new File(srcPath);
		try {
			if (srcFile.exists()) {
				ZipOutputStream zipOut;
				if (dstPath.endsWith(".zip") || dstPath.endsWith(".ZIP")) {
					zipOut = new ZipOutputStream(new BufferedOutputStream(
							new FileOutputStream(dstPath)));

					zipOut = new ZipOutputStream(new FileOutputStream(dstPath));
				} else {
					File file = new File(dstPath);
					if (!file.exists())
						file.mkdirs();
					zipOut = new ZipOutputStream(new FileOutputStream(new File(
							file, srcFile.getName() + ".zip")));
				}
				zip(srcFile, zipOut, "");
				zipOut.close();
			} else {
				System.out
						.println("file or dir isn't existent(FileOperate.zip())");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void zip(File file, ZipOutputStream zip, String entryName) {
		try {
			if (file.isDirectory()) {

				ZipEntry ze = new ZipEntry(entryName +

				file.getName() + "/");
				ze.setTime(file.lastModified());
				zip.putNextEntry(ze);
				zip.closeEntry();
				String[] names = file.list();
				for (int i = 0; i < names.length; i++) {
					zip(new File(file, names[i]), zip, entryName
							+ file.getName() + "/");
				}
			} else {
				int len;
				byte[] buf = new byte[1024];
				BufferedInputStream in = new BufferedInputStream(
						new FileInputStream(file), 1024);
				ZipEntry ze = new ZipEntry(entryName + file.getName());
				ze.setTime(file.lastModified());
				zip.putNextEntry(ze);
				while ((len = in.read(buf)) != -1)
					zip.write(buf, 0, len);
				zip.flush();
				in.close();
				zip.closeEntry();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
分享到:
评论

相关推荐

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

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

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

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

    CANoe /CAPL 文件操作脚本

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

    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中获取即可。

    js实现读写文件操作

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

    JSP文件操作

    JSP文件操作

    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`方法获取当前...

    使用C语言文件操作模拟实现简易记事本

    使用C语言文件操作模拟实现简易记事本,可以达到创建文件,打开文件,读取文件等操作,主要在于熟悉使用C语言的文件操作相关函数,以及对于一些cmd命令同C语言的结合的使用,并且可以通过自定义函数,开关分支语句等...

    VC之PDF文件操作

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

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

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

    实现简单的文件系统

    通过对具体的文件存储空间的管理、文件的物理结构、目录结构和文件操作的实现,加深对文件系统内部功能和实现过程的理解。 要求: 1.在内存中开辟一个虚拟磁盘空间作为文件存储器,在其上实现一个简单的单用户文件...

    C语言文件操作基础:打开与关闭文件的详解与实践

    在C语言中,打开和关闭文件是进行文件操作的基础。通过使用fopen和fclose函数,程序员可以控制文件的打开和关闭,从而进行有效的文件读写操作。了解文件打开模式、错误处理、文件缓冲区以及二进制文件操作的相关知识...

Global site tag (gtag.js) - Google Analytics