检索目录下是否有重名文件 并返回不重名字符串 Utils

本文介绍了一个实用工具方法,用于检索指定目录下是否存在重名文件,并返回一个确保不重复的文件名字符串。
摘要由CSDN通过智能技术生成


package cn.itcast.tmhkcUtils;

import java.io.File;

public class MyFileUtils {

	// 获取文件后缀名
	public static String getFileExt(String s) {
		String s1 = new String();
		int i = 0;
		int j = 0;
		if (s == null)
			return null;
		i = s.lastIndexOf(".") + 1;
		j = s.length();
		s1 = s.substring(i, j);
		if (s.lastIndexOf(46) > 0)
			return s1.toLowerCase();
		else
			return "";
	}

	// 获取文件名的名称
	public static String getNameWithoutExtension(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf("."));
	}
	/**
	 *
	 * @param sPath 需要检查的文件夹路径字符串
	 * @param sFile 文件的全称 	 格式为*.***
	 * @return 返回一个不重复的文件名全称 格式为  *.*
	 * @throws 
	 */
	public static String newFile(String sPath, String sFile) throws Exception {
		if (!sFile.contains(".")) 
			throw new Exception("输入的文件名格式不正确,应该为*.*");
		String newFileName = new String();
		String 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值