亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

java(包括springboot)讀取resources下文件方式實(shí)現(xiàn)

瀏覽:2日期:2022-08-24 15:12:33

本文主要介紹了java(包括springboot)讀取resources下文件方式實(shí)現(xiàn),分享給大家,具體如下:

java(包括springboot)讀取resources下文件方式實(shí)現(xiàn)

1、使用項(xiàng)目?jī)?nèi)路徑讀取,該路徑只在開發(fā)工具中顯示,類似:src/main/resources/resource.properties。只能在開發(fā)工具中使用,部署之后無法讀取。(不通用)

File file = new File('src/main/resources/resource.properties');

@Test public void testReadFile2() throws IOException { File file = new File('src/main/resources/resource.properties'); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); fis.close(); }

2、使用org.springframework.util.ResourceUtils,讀取。在linux環(huán)境中無法讀取。(不通用)

File file = ResourceUtils.getFile('classpath:resource.properties');FileInputStream fis = new FileInputStream(file);

@Test public void testReadFile3() throws IOException { File file = ResourceUtils.getFile('classpath:resource.properties'); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); fis.close(); }

3、使用org.springframework.core.io.ClassPathResource,各種環(huán)境都能讀取。(通用)

Resource resource = new ClassPathResource('resource.properties');InputStream is = resource.getInputStream();

@Test public void testReadFile() throws IOException {// ClassPathResource classPathResource = new ClassPathResource('resource.properties'); Resource resource = new ClassPathResource('resource.properties'); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); is.close(); }

4、結(jié)合spring注解,使用org.springframework.core.io.ResourceLoader;類的注解。(通用)

package com.tsinkai.ettp;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.core.io.Resource;import org.springframework.core.io.ResourceLoader;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class EttpCustomApplicationTests { @Autowired ResourceLoader resourceLoader; @Test public void testReaderFile() throws IOException { Resource resource = resourceLoader.getResource('classpath:resource.properties'); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); is.close(); }}

到此這篇關(guān)于java(包括springboot)讀取resources下文件方式實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java 讀取resources內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
上一條:詳解Java中native方法的使用下一條:
相關(guān)文章:
主站蜘蛛池模板: 亚洲欧美自拍一区 | 91视频最新网站 | 玖玖精品国产 | 亚洲国产品综合人成综合网站 | 免费一极毛片 | 国产成人免费高清在线观看 | 欧美成人午夜毛片免费影院 | 黄色网址亚洲 | 久久99亚洲精品久久99 | 欧美视频网站在线观看 | 免费看欧美日韩一区二区三区 | 亚洲理论欧美理论在线观看 | 国产欧美日韩精品第三区 | 欧美日韩在线观看区一二 | 日本韩国欧美一区 | 亚洲国产人成中文幕一级二级 | 男女男精品视频免费观看 | 亚洲手机看片 | 日一区二区 | 亚洲va久久久噜噜噜久久狠狠 | 草草影院地址 | 极品欧美| 一级a毛片免费观看久久精品 | 欧美一级毛片高清免费观看 | 久色小说 | 午夜手机视频 | 久久91精品国产91久久小草 | 美女很黄很黄免费 | 久久午夜精品 | 精品国产视频在线观看 | 在线亚洲自拍 | 国产在线91精品 | 国产精品嘿咻嘿咻在线播放 | 国产精品高清在线观看93 | 国产精品欧美一区二区 | 在线成人免费看大片 | 午夜宅男在线永久免费观看网 | 亚洲国产成人麻豆精品 | 久久福利资源网站免费看 | 国内精品久久久久久久影视麻豆 | 精品视频一区二区三区免费 |