`

文件操作

 
阅读更多
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内...

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

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

    VC之PDF文件操作

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

    LabVIEW文件操作_数据存储 VI源程序

    在“LabVIEW文件操作_数据存储 VI源程序”这个主题中,我们将深入探讨如何使用LabVIEW进行文件操作,特别是数据的读取与存储。 一、LabVIEW的数据存储 1. **电子表格(Excel)文件操作**:LabVIEW提供了强大的...

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

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

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

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

    实现简单的文件系统

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

Global site tag (gtag.js) - Google Analytics