linux和window读取文件遇到的坑


最开始读写文件我都是用的FileInputStream、FileOutputStream在window系统测试完全没问题,jar包上传到linux直接说找不到文件,后面用ClassPathResource、FileSystemResource读写文件保证两个系统都适用。
代码里那种写方式会在项目根路径生成相同名称的文件,这个文件在代码执行结束后就没用了,直接删除。废话不多说,直接上代码,下面是正确无误的代码:

package com.common.util;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.WritableResource;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.Map;

@Slf4j
public class YmlUtil {

    /**
     * 解析yml配置文件:读写操作
     */
    public static void resolveDefaultZone() {
        //加解密工具类
        StandardPBEStringEncryptor stringEncryptor = new StandardPBEStringEncryptor();
        Yaml yaml = new Yaml();
        InputStream inputStream = null;
        OutputStream os = null;
        try {
            //读操作:读取项目resource目录下的bootstrap.yml文件  不要用FileInputStream这种方式linux系统读取不到文件
            ClassPathResource resource = new ClassPathResource("bootstrap.yml");
            inputStream = resource.getInputStream();
            Map map = yaml.load(inputStream);
            //已经读取到文件,根据文件内容读取数据
            String jasyptPwd = (String) ((Map) ((Map) map.get("jasypt")).get("encryptor")).get("password");
            stringEncryptor.setPassword(jasyptPwd);
            Map eurekaMap = (Map) map.get("eureka");
            Map registeraccountMap = (Map) eurekaMap.get("registeraccount");
            Map<String, String> userMap = (Map) registeraccountMap.get("user");
            String registeraccountName = userMap.get("name");
            String registeraccountPwd = userMap.get("password");
            if (registeraccountName.contains("ENC(")) {
                userMap.put("name", stringEncryptor.decrypt(registeraccountName.replace("ENC(", "").replace(")", "")));
            }
            if (registeraccountPwd.contains("ENC(")) {
                userMap.put("password", stringEncryptor.decrypt(registeraccountPwd.replace("ENC(", "").replace(")", "")));
            }
            //FileSystemResource获取输出流  不要用FileOutputStream这种方式linux系统读取不到文件
            WritableResource res = new FileSystemResource("bootstrap.yml");
            os = res.getOutputStream();
            //写操作:会在项目根路径生成文件
            yaml.dump(map, new OutputStreamWriter(os));
        } catch (Exception e) {
            log.error("解析bootstrap.yml失败!errMsg:{}", e.getMessage());
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        }
    }
    public static void deleteYml() {
        //删除项目根路径下生成的无用文件
        String property = System.getProperty("user.dir");
        File file = new File(property+"/bootstrap.yml");
        if (file.exists()) {
            file.delete();
        }
    }
}