午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

java(包括springboot)讀取resources下文件方式實現

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

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

java(包括springboot)讀取resources下文件方式實現

1、使用項目內路徑讀取,該路徑只在開發工具中顯示,類似:src/main/resources/resource.properties。只能在開發工具中使用,部署之后無法讀取。(不通用)

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環境中無法讀取。(不通用)

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,各種環境都能讀取。(通用)

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、結合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(); }}

到此這篇關于java(包括springboot)讀取resources下文件方式實現的文章就介紹到這了,更多相關java 讀取resources內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 尤物网站在线 | 激情五月婷婷综合 | 国产一区在线观看免费 | 操操操视频 | 男人天堂av在线播放 | 欧美videos另类精品 | 免费黄色成人 | 欧美另类第一页 | 麻豆一二区 | 久久久久久91香蕉国产 | 成人精品av | 国产精品久久精品 | 日韩乱论| 国内自拍真实伦在线观看 | 成人短视频在线观看 | 香蕉av网 | 精品免费一区二区三区 | 亚洲视频精品 | 欧美一级免费 | 欧美区亚洲区 | 欧美三级免费看 | 成人精品在线观看 | 亚洲一久久 | 狼人伊人av| 中文字幕日韩欧美 | 久久一区二| 国产精品13p | av中文天堂在线 | 又紧又大又爽精品一区二区 | 午夜影院一区 | av国产在线观看 | 午夜毛片在线 | 欧美专区在线 | 丁香婷婷在线 | 久久国产精品网站 | 91网站在线看 | 超碰人人91| av国产在线观看 | 黄色网页在线播放 | 成年人久久 | 黄色高清在线观看 |