java(包括springboot)讀取resources下文件方式實(shí)現(xiàn)
本文主要介紹了java(包括springboot)讀取resources下文件方式實(shí)現(xiàn),分享給大家,具體如下:
1、使用項(xiàng)目內(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)!
相關(guān)文章:
1. vue實(shí)現(xiàn)web在線聊天功能2. JavaScript實(shí)現(xiàn)頁面動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)示例3. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis4. Springboot 全局日期格式化處理的實(shí)現(xiàn)5. SpringBoot+TestNG單元測試的實(shí)現(xiàn)6. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題7. 解決Android Studio 格式化 Format代碼快捷鍵問題8. 在Chrome DevTools中調(diào)試JavaScript的實(shí)現(xiàn)9. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼10. Java使用Tesseract-Ocr識(shí)別數(shù)字
