SpringBoot打成jar运行后无法读取resources里的文件

SpringBoot打成jar运行后无法读取resources里的文件

项目中涉及到java读写txt里的数据,本地用的是文件相对路径,正常运行。打成jar包后运行,报错java.io.FileNotFoundException ,应该是jar里面的资源文件, 在磁盘上是没有真实路径存在的, 所以通过文件读取方式会报错。可以通过I/O流的方式先获取这个文件流, 再将这个文件流写入到一个真实存在的指定磁盘路径, 这样我们就可以通过 new File() 读取文件的方式访问了。
以下几种方式:

采用绝对路径的方式

采用的是绝对路径的方式,获取linux环境下的存放文件的具体路径。如:(/usr/local/)。
这种方式是先在application.yml设置好路径,比如:

path:
  resourcePath: /usr/local/data

然后再通过@Value获取

@Value("${path.resourcePath}")
private static String resourcePath;

我用的是后面两种。

通过ClassPathResource

用压缩软件打开 jar 文件,看看txt文件位于jar内部的路径
在这里插入图片描述
通过 ClassPathResource resource = new ClassPathResource(“test.txt”);方式读取jar中的文件流

ClassPathResource resource = new ClassPathResource("test.txt");
File sourceFile = resource.getFile();
InputStream fis = resource.getInputStream();

还要在项目pom.xml中配置resources情况

<build>
		<!-- 定义包含这些资源文件,能在jar包中获取这些文件 -->
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
					<include>**/*.yml</include>
				</includes>
				<!--是否替换资源中的属性-->
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
				<!--是否替换资源中的属性-->
				<filtering>false</filtering>
			</resource>
		</resources>
	</build>

我用的是resource.getInputStream();

 try {
            ClassPathResource resource = new ClassPathResource(filename);
            InputStream fis = resource.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String line = null;
            while ((line = br.readLine()) != null) {
            String[] datas = line.split(",");
            svm_node[] vector = new svm_node[datas.length - 1];
            for (int i = 0; i < datas.length - 1; i++) {
            svm_node node = new svm_node();
            node.index = i + 1;
            node.value = Double.parseDouble(datas[i]);
            vector[i] = node;
        }

通过getClassLoader

this.getClass().getClassLoader().getResourceAsStream(“iris.txt”);获取流后也可以读取出来。

StringBuffer stringBuffer = new StringBuffer();
        try {
            InputStream stream = this.getClass().getClassLoader().getResourceAsStream("iris.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                stringBuffer.append(line);
                stringBuffer.append(",");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取文件iris"+e);
        }

        IrisFile irisFile = new IrisFile(stringBuffer);

读解决了,写呢?

我这测试写是没有影响的,例如,追加和清除txt里的数据。

//追加文件内容
    public static void txtadd(String file, String conent) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(conent+"\r\n");//换行
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    //清空文件内容
    public void clearInfoForFile(String fileName) {
        File file = new File(fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值