Java文件操作功能

个人记录:文件操作的实现,主语是使用Java中File的。它主要操作实在磁盘,跟上传和下载有区别,上传和下载都要经过浏览器。因此,上传下载还要考虑到http的小部分知识

这里也使用了IO流。Java的IO流我在博客园有发布过。https://www.cnblogs.com/gaolt/p/9814723.html

1、复制文件和文件夹:文件夹使用到递归

2、剪切文件和文件夹:文件夹使用到递归

   1)代码中使用到两种方法。

       (1)如果同名的文件在指定目录存在了,就使用IO流中的channel的transferTo这个方法来进行剪切。再删除源文件

        (2)如果同名文件在指定目录不存在,就使用复制的方法。进行先复制,后删除源文件。

3、删除文件和文件夹:使用File中的delete()方法。删除文件夹的时候,有子级文件夹,就使用到递归

4、创建文件夹:mkdirs()。创建文件夹,如果父级文件夹不存在,就连同父级一同创建。跟mkdir()区别是,或者不创建父级

5、重命名文件和文件夹:使用File的renameTo()方法,方法说明:如果指定目录有没有相同文件名的文件就时剪切,如果有就是重命名。

6、生成文件名:这里生成的文件名,跟window一样的实现。根据个人创建重复文件和文件夹时想到,并写下代码


	/**
	 * 删除文件或文件夹
	 *
	 * @param src 源文件或文件夹绝对路径
	 * @return 是否成功删除文件或文件夹
	 */
	public static boolean delete(File src) {
		if (!src.exists()) {
			return false;
		}
		if (src.isFile()) {
			src.delete();
		} else {
			for (File srcSub : src.listFiles()) {
				delete(srcSub);// 递归删除源文件夹下子文件和文件夹
			}
			src.delete();
		}
		return true;
	}

	/**
	 * 重命名文件或文件夹
	 * 
	 * @param src     要重命名的文件或文件夹对象
	 * @param newName 新名字
	 * @return
	 */
	public static boolean renameFile(File src, String newName) {
		String srcPath = src.getParent();
		File newFile = new File(srcPath, newName);
		boolean b = src.renameTo(newFile);
		if (b) {
			return true;
		}
		return false;

	}

	/**
	 * 文件夹和文件复制的公共方法 如果文件夹中存在文件或文件夹都会被复制过来
	 * 
	 * @param src    源文件或文件夹绝对路径
	 * @param dstDir 目标文件夹绝对路径
	 * @return 是否成功复制文件或文件夹
	 */
	@SuppressWarnings("resource")
	public static boolean copy(File docuOrFileSrc, File dstDir) {
		String newFileName = "";
		String fileName = docuOrFileSrc.getName();
		if (!docuOrFileSrc.exists()) {
			return false;
		}
		if (dstDir.isFile()) {
			return false;
		}
		if (!dstDir.exists()) {
			dstDir.mkdirs();
		}
		if (docuOrFileSrc.isFile()) {// 文件
			copyDocu(docuOrFileSrc, dstDir);
		} else {// 文件夹
			String oldSrcName = docuOrFileSrc.getName();
			File newSrcDir = new File(dstDir, oldSrcName);
			if (newSrcDir.exists()) {
				String savepath = dstDir.getAbsolutePath();
				newFileName = makeFileName(savepath, fileName, 0);
			} else {
				newFileName = oldSrcName;
			}
			File newPathFile = new File(dstDir, newFileName);
			if (!newPathFile.exists()) {
				newPathFile.mkdirs();
			}
			for (File srcSub : docuOrFileSrc.listFiles()) {
				copy(srcSub, newPathFile);// 递归复制源文件夹下子文件和文件夹
			}

		}
		return true;
	}

	/**
	 * 移动(剪切)文件或文件夹
	 * 
	 * @param src    源文件或文件夹绝对路径
	 * @param dstDir 目标文件夹绝对路径
	 * @return 是否成功移动文件或文件夹
	 */
	public static boolean move(File docuOrFileSrc, File dstDir) {
		String newFileName = "";
		if (!docuOrFileSrc.exists()) {
			return false;
		}
		if (dstDir.isFile()) {
			return false;
		}
		if (!dstDir.exists()) {
			dstDir.mkdirs();
		}
		if (docuOrFileSrc.isFile()) {// 文件
			moveDocu(docuOrFileSrc, dstDir);
		} else {// 文件夹
			String oldSrcName = docuOrFileSrc.getName();
			String savePath = dstDir.getAbsolutePath();
			File newSrcDir = new File(dstDir, oldSrcName);
			if (newSrcDir.exists()) {
				newFileName = makeFileName(savePath, oldSrcName, 0);
			} else {
				newFileName = oldSrcName;
			}
			File newFile = new File(savePath, newFileName);
			newFile.mkdirs();
			for (File srcSub : docuOrFileSrc.listFiles()) {
				move(srcSub, newSrcDir);// 递归移动源文件夹下子文件和文件夹
			}
			docuOrFileSrc.delete();
		}
		return true;
	}

	/**
	 * 复制单个文件
	 * 
	 * @param docuSrc
	 * @param dstDir
	 * @return
	 */
	@SuppressWarnings("resource")
	public static boolean copyDocu(File docuSrc, File dstDir) {
		String realName = "";
		if (!docuSrc.exists() || docuSrc.isDirectory()) {
			return false;
		}
		if (!dstDir.exists()) {
			dstDir.mkdirs();
		}
		String oldFileName = docuSrc.getName();
		File srcfile = new File(dstDir, oldFileName);
		if (srcfile.exists()) {
			String savePath = dstDir.getAbsolutePath();
			realName = makeFileName(savePath, oldFileName, 0);
		} else {
			realName = oldFileName;
		}
		File newFile = new File(dstDir, realName);
		try {
			FileChannel fileIn = new FileInputStream(docuSrc).getChannel();
			FileChannel fileOut = new FileOutputStream(newFile).getChannel();
			fileIn.transferTo(0, fileIn.size(), fileOut);
			fileIn.close();
			fileOut.close();
		} catch (IOException e) {
			return false;
		}
		return true;
	}

	/**
	 * 移动(剪切)文件(单个文件)
	 *
	 * @param docuSrc 要移动的源文件
	 * @param dstDir  移动到的指定文件夹
	 * @return
	 */
	@SuppressWarnings("resource")
	public static boolean moveDocu(File docuSrc, File dstDir) {
		String oldFileName = docuSrc.getName();
		if (!docuSrc.exists() || docuSrc.isDirectory()) {
			return false;
		}
		if (!dstDir.exists()) {
			dstDir.mkdirs();
		}

		File dstFile = new File(dstDir, oldFileName);
		if (dstFile.exists()) {// 直接重命名绝对路径速度更快.renameTo()这个方法说明:如果保存目录存在就重命名,如果不存在就剪切
			try {
				FileChannel filein = new FileInputStream(docuSrc).getChannel();
				FileChannel fileout = new FileOutputStream(dstFile).getChannel();
				filein.transferTo(0, filein.size(), fileout);
				filein.close();
				fileout.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {// 文件已经不存在
			copy(docuSrc, dstDir);
		}
		docuSrc.delete();
		return true;
	}

	/**
	 * 创建文件夹
	 * 
	 * @param path
	 */
	public static boolean createFolder(File srcFile) {
		boolean b = srcFile.mkdirs();
		if (b) {
			return true;
		}
		return false;
	}

	/**
	 * 生成文件名:在相同文件夹内不允许相同的文件名存在
	 * 
	 * @param savepath 保存文件的路径
	 * @param fileName 文件名,带文件类型
	 * @param num      记录第几个文件同名了
	 * @return
	 */
	public static String makeFileName(String savepath, String fileName, int num) {
		int count = 2;
		String realName = "";
		String ExFileName = "";
		String newFileName = fileName;
		File saveFile = new File(savepath, fileName);
		if (fileName.contains(".")) {
			ExFileName = fileName.substring(fileName.lastIndexOf(".") + 1);
		}
		StringBuffer sbf = new StringBuffer();
		if (num >= 2) {
			count = num + 1;
		}
		if (saveFile.exists()) {
			if (fileName.contains("(")) {
				realName = fileName.substring(0, fileName.indexOf("(")).trim();
			} else {
				if (ExFileName == null || ExFileName.equals("")) {
					realName = fileName;
				} else {
					realName = fileName.substring(0, fileName.lastIndexOf("."));
				}
			}
			sbf.append(realName);
			sbf.append(" (" + count + ")");
			if (ExFileName != null && !ExFileName.equals("")) {
				sbf.append(".");
			}
			sbf.append(ExFileName);
			newFileName = sbf.toString();
			// 使用递归
			num = count;

			newFileName = makeFileName(savepath, newFileName, num);
		}
		return newFileName;
	}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值