整理近来用到的几个方法。文件拷贝,文件改名,对象比对等

1.修改路径下某个具体文件的文件名

/**
     * 功能描述:
     * 〈修改文件名,传入两个文件路径参数〉
     *
     * @param filePath 原文件绝对路径
     * @param newFilePath 被修改后文件绝对路径
     * @return : java.lang.String
     * @author : zach
     * @date : 2021/1/14 11:37
     */
    private static String FixFileName(String filePath, String newFilePath) {
        File file = new File(filePath);
        if (!file.exists()) { // 判断原文件是否存在(防止文件名冲突)
            return null;
        }
        newFilePath = newFilePath.trim();
        if ("".equals(newFilePath) || newFilePath == null) // 文件名不能为空
            return null;
        File nfile = new File(newFilePath);
        try {
            file.renameTo(nfile); // 修改文件名
        } catch (Exception err) {
            err.printStackTrace();
            return null;
        }
        return newFilePath;
    }

2.通过java根据路径进行文件的拷贝

 /**
     * 功能描述:
     * 〈传入参数,将一个文件已固定命名,拷贝一份,可指定路径〉
     *
     * @param fileSourcePath 源文件绝对路径
     * @param fileTargetPath 拷贝后新文件绝对路径
     * @param newFileName 新文件名称(需要自己写入后缀)
     * @return : void
     * @author : zach
     * @date : 2021/1/13 13:59
     */
    public static void FileCopyUtil(String fileSourcePath,String fileTargetPath,String newFileName){
        File sourceFile = new File(fileSourcePath);
        File targetPath = new File(fileTargetPath);
        try (FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(new File(targetPath.getAbsoluteFile() + File.separator + newFileName));){
        byte[] bytes = new byte[1024];  // 使用字节数组拷贝
        int len = 0;// 定义变量用于接收输入流读取一个数据字节
        while ((len = fis.read(bytes)) != -1) {// 循环读入,len为-1表示文件已经读取完毕
            fos.write(bytes, 0, len);// 用字节数组输出流循环写入数据(注意:要拷贝的是每次获取的字节数组的长度)
            fos.flush();// 刷新保证数据写入到文件中
        }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.使用java获取部分系统参数

public class SystemType {
    public static void main(String[] args) {
        System.out.println("===========os.name:"+System.getProperties().getProperty("os.name"));//操作系统的名称
        System.out.println("===========java.vendor :"+System.getProperties().getProperty("java.vendor "));//Java 运行时环境供应商
        System.out.println("===========java.version:"+System.getProperties().getProperty("java.version"));//Java 运行时环境版本
        System.out.println("===========java.vendor.url:"+System.getProperties().getProperty("java.vendor.url"));//Java 供应商的 URL
        System.out.println("===========java.home:"+System.getProperties().getProperty("java.home"));//Java 安装目录
        System.out.println("===========java.vm.specification.version:"+System.getProperties().getProperty("java.vm.specification.version"));//Java 虚拟机规范版本
        System.out.println("===========java.vm.specification.vendor:"+System.getProperties().getProperty("java.vm.specification.vendor"));//Java 虚拟机规范供应商
        System.out.println("===========java.vm.specification.name:"+System.getProperties().getProperty("java.vm.specification.name"));//Java 虚拟机规范名称
        System.out.println("===========java.vm.version:"+System.getProperties().getProperty("java.vm.version"));//Java 虚拟机实现版本
        System.out.println("===========java.vm.vendor:"+System.getProperties().getProperty("java.vm.vendor"));//Java 虚拟机实现供应商
        System.out.println("===========java.vm.name:"+System.getProperties().getProperty("java.vm.name"));//Java 虚拟机实现名称
        System.out.println("===========java.specification.version:"+System.getProperties().getProperty("java.specification.version"));//Java 运行时环境规范版本
        System.out.println("===========java.specification.vendor:"+System.getProperties().getProperty("java.specification.vendor"));//Java 运行时环境规范供应商
        System.out.println("===========java.specification.name:"+System.getProperties().getProperty("java.specification.name"));//Java 运行时环境规范名称
        System.out.println("===========java.class.version:"+System.getProperties().getProperty("java.class.version"));//Java 类格式版本号
        System.out.println("===========java.class.path:"+System.getProperties().getProperty("java.class.path"));//Java 类路径
        System.out.println("===========java.library.path:"+System.getProperties().getProperty("java.library.path"));//加载库时搜索的路径列表
        System.out.println("===========java.io.tmpdir:"+System.getProperties().getProperty("java.io.tmpdir"));//默认的临时文件路径
        System.out.println("===========java.compiler:"+System.getProperties().getProperty("java.compiler"));//要使用的 JIT 编译器的名称
        System.out.println("===========java.ext.dirs:"+System.getProperties().getProperty("java.ext.dirs"));//一个或多个扩展目录的路径
        System.out.println("===========os.arch:"+System.getProperties().getProperty("os.arch"));//操作系统的架构
        System.out.println("===========os.version:"+System.getProperties().getProperty("os.version"));//操作系统的版本
        System.out.println("===========file.separator:"+System.getProperties().getProperty("file.separator"));//文件分隔符(在 UNIX 系统中是“/”)
        System.out.println("===========path.separator:"+System.getProperties().getProperty("path.separator"));//路径分隔符(在 UNIX 系统中是“:”)
        System.out.println("===========line.separator:"+System.getProperties().getProperty("line.separator"));//行分隔符(在 UNIX 系统中是“/n”)
        System.out.println("===========user.name:"+System.getProperties().getProperty("user.name"));//用户的账户名称
        System.out.println("===========user.home:"+System.getProperties().getProperty("user.home"));//用户的主目录
        System.out.println("===========user.dir:"+System.getProperties().getProperty("user.dir"));//用户的当前工作目录
    }
}

4.java中,同对象进行属性比对

/**
	 * 功能描述:
	 * 〈获取传入的Map的Key数据数组〉
	 *
	 * @param contrastMap 传入的Map数据
	 * @param rtnList 传入和返回的List
	 * @return : java.util.List<java.lang.String>
	 * @author : zach
	 * @date : 2020/12/18 11:43
	 */
	public static List<String> ContrastAndAdd(Map<String,Map<String,Object>> contrastMap,List<String> rtnList){
		Iterator<String> it = contrastMap.keySet().iterator();
		while(it.hasNext()){
			rtnList.add(it.next());
		}
		return rtnList;
	}

	/**
	 * 功能描述:
	 * 〈比较两个实体属性值,返回一个map以有差异的属性名为key,value为一个Map分别存oldObject,newObject此属性名的值〉
	 *
	 * @param oldObject 进行属性比较的对象1
	 * @param newObject 进行属性比较的对象2
	 * @return : java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>
	 * @author : zach
	 * @date : 2020/12/18 10:05
	 */
	public static Map<String, Map<String,Object>> compareFields(Object oldObject, Object newObject) {
		Map<String, Map<String, Object>> map = null;
		try{
			/**
			 * 只有两个对象都是同一类型的才有可比性
			 */
			if (oldObject.getClass() == newObject.getClass()) {
				map = new HashMap<String, Map<String,Object>>();
				Class clazz = oldObject.getClass();
				//获取object的所有属性
				PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,Object.class).getPropertyDescriptors();
				for (PropertyDescriptor pd : pds) {
					//遍历获取属性名
					String name = pd.getName();
					//获取属性的get方法
					Method readMethod = pd.getReadMethod();
					// 在oldObject上调用get方法等同于获得oldObject的属性值
					Object oldValue = readMethod.invoke(oldObject);
					// 在newObject上调用get方法等同于获得newObject的属性值
					Object newValue = readMethod.invoke(newObject);

					if(oldValue instanceof List){
						continue;
					}
					if(newValue instanceof List){
						continue;
					}
					if(oldValue instanceof Timestamp){
						oldValue = new Date(((Timestamp) oldValue).getTime());
					}
					if(newValue instanceof Timestamp){
						newValue = new Date(((Timestamp) newValue).getTime());
					}
					if(oldValue == null && newValue == null){
						continue;
					}else if(oldValue == null && newValue != null){
						Map<String,Object> valueMap = new HashMap<String,Object>();
						valueMap.put("oldValue",oldValue);
						valueMap.put("newValue",newValue);
						map.put(name, valueMap);
						continue;
					}
					if (!oldValue.equals(newValue)) {// 比较这两个值是否相等,不等就可以放入map了
						Map<String,Object> valueMap = new HashMap<String,Object>();
						valueMap.put("oldValue",oldValue);
						valueMap.put("newValue",newValue);
						map.put(name, valueMap);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}

以上都是公司项目中实际测试过没有问题的方法,可以拿去直接用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值