`

文件操作

阅读更多
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();
		}
	}
}
分享到:
评论

相关推荐

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

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

    读写文件读取文件操作读取文件操作读取文件操作读取文件操作

    读取文件操作读取文件操作读取文件操作读取文件操作读取文件操作读取文件操作读取文件操作读取文件操作读取文件操作读取文件操作读取文件操作

    C++文件操作 C++ 文件操作

    很详细的C++文件操作 C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifstream: 读操作(输入)的文件类(由istream引申而来) fstream: 可同时读写操作的文件类 ...

    linux文件操作 linux操作系统 文件操作 常用命令

    linux文件操作 linux操作系统 文件操作 常用命令 系统命令

    C++ 文件操作大全

    C++ 文件操作大全,里面列举了几乎所有的C++文件操作以及相关细节。

    MFC文件操作详解

    MFC文件操作各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的。...

    电子科技大学linux环境编程作业2——李林——编写带缓存的文件操作类

    编写带缓存的文件操作类 从执行体程序库中的CLLogger类可知,通过缓存要写入文件中的数据,能够提高读写磁盘的性能 请编写一个文件操作的封装类,其要求如下: 需要提供open/read/write/lseek/close等函数的封装函数...

    c#文件操作方法大全

    c#对文件或者文件夹全面的操作。以及c#对文件操作方面的一些属性的概述。

    python文件操作实验报告.doc

    python文件操作

    js对本地文件操作

    js对本地文件操作,仅限IE浏览器使用,其它浏览器不兼容(ActiveXObject方法)

    codesys工程ST语言写文件操作 TXT文件。

    本工程笔者使用ST语言实现文件的写操作,详细见本人博客,内容笔者亲测有效。后续更新读文件操作

    C语言的文件操作 C语言文件操作

    C语言文件操作 C语言文件操作 C语言文件操作

    Python文件操作(课件)

    详细介绍Python中的文件操作,包括文件操作的各种模式分析、文件夹的递归访问、Excel文件的读取和写入等,并通过具体示例演示说明,非常适合高校老师教学和学生复习使用。

    C语言文件操作及函数大全

    C语言文件操作及函数大全 2.文件操作函数: (1)文件打开函数fopen fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen("文件名","使用文件方式"); 其中,“文件指针名”必须是被说明为FILE 类型的...

    文件操作函数大全文件操作函数大全

    文件操作函数大全文件操作函数大全文件操作函数大全文件操作函数大全文件操作函数大全文件操作函数大全文件操作函数大全文件操作函数大全

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

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

    C语言文件操作(文件操作)

    C语言文件操作(文件操作) FILE *p 那些的文件操作 我经常做些大型系统需要很多文件操作

    winform作的文件操作学习程序

    用winform作的文件操作程序,适合初学者入门学习,主要功能有:创建文件,删除文件,复制文件等等以及目录操作,文件的写入,读取,还有二进制文件的操作

    java文件操作类

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

    C++流类体系与文件操作

    通过本章学习,应理解I/O流、流类与流类体系的概念,...了解C++有关文件的概念及文件的使用方法,理解文件流类体系结构,掌握实现文件操作的成员函数的使用方法,学会文本文件的打开、读/写、关闭等操作的编程方法。

Global site tag (gtag.js) - Google Analytics