java读取resource目录下的properties或txt文档笔记
发布日期:2022-02-21 17:40:26
浏览次数:47
分类:技术文章
本文共 1096 字,大约阅读时间需要 3 分钟。
假设Resource文件夹下有两文件:
- demo1.properties
demo1内容:
name= chenhao age= 23 - demo2.txt
public class ReadFromResourceFolder{ // 读取properties文件 public static void readProperties() throws IOException { //读取properties类型文件 ResourceBundle bundle = ResourceBundle.getBundle("demo1"); System.out.println(bundle.getString("name")); Demo1 demo = new Demo1(); demo.readResources(); } // 读取txt文件 public void readResources() throws IOException{ InputStream inputStream = this.getClass().getResourceAsStream("/demo2.txt"); BufferedReader reader = null; Listlist = new ArrayList<>(); try{ reader = new BufferedReader(new InputStreamReader(inputStream)); String str = null; while((str = reader.readLine()) != null){ //按行读取文件内容 list.add(str); } System.out.println(list)); }catch ( Exception e){ e.printStackTrace(); } }}
需要注意的是:txt文件需要直接在resource文件夹下才能被读取到。
转载地址:https://blog.csdn.net/weixin_40597409/article/details/121374086 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年03月23日 21时23分02秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
程序员编程的 7 + 1 条小贴士
2019-04-26
2、DispatcherServlet in Spring MVC
2019-04-26
如何下载Spring
2019-04-26
内存递归生成树形结构
2019-04-26
Guava collections -- FluentIterable
2019-04-26
Guava collections -- Lists
2019-04-26
Guava collections -- Sets
2019-04-26
Guava collections -- Maps
2019-04-26
Guava collections -- Multimaps
2019-04-26
Guava collections -- BiMap
2019-04-26
Guava collections -- Table
2019-04-26
Guava collections -- Range
2019-04-26
Guava collections -- Immutable Collections
2019-04-26
Guava collections -- Ordering
2019-04-26
成为Java顶尖程序员 ,看这11本书就够了
2019-04-26
Spring AOP 动态多数据源
2019-04-26
3、Spring MVC 之 一些特殊的Bean
2019-04-26
Java JDK Integer Cache
2019-04-26
4、Spring MVC 之 实现Controller
2019-04-26
Spring MVC @RequestMapping
2019-04-26