java resources 读取文件_java读取Resources下文件

java读取Resources下文件

第一种,读为流文件

InputStream path = this.getClass().getResourceAsStream("/data.txt");

BufferedReader reader = new BufferedReader(new InputStreamReader(path));

1

2

第二种, 读取路径

Thread.currentThread().getContextClassLoader()。

URL l1 =

Thread.currentThread().getContextClassLoader().getResource("readFile/test1.xml");

System.out.println(l1);

URL l2 =

Thread.currentThread().getContextClassLoader().getResource("collection/test2.xml");

System.out.println(l2);

URL l3 = Thread.currentThread().getContextClassLoader().getResource("test3.xml");

String l4=l3.getPath();//加上getPath()则去掉前面的file:

System.out.println(l3);  //     file:/F:/demo/Studying/out/production/Studying/test3.xml

System.out.println(l4);//      /F:/demo/Studying/out/production/Studying/test3.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

第三种, 读为流文件

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/job_two_one_mapping.txt");

try(Scanner scanner = new Scanner(is)) {

while (scanner.hasNextLine()) {

System.out.println(scanner.nextLine());

}

} catch (Exception e) {

log.error("读取文件数据异常" ,e);

}

1

2

3

4

5

6

7

8

第四种, 读取路径

ResourceBundle bundle = ResourceBundle.getBundle("config");

String url = bundle.getString("url");

1

2

该方法默认读取的是resources文件夹下的以.properties为后缀的文件,代码中的例子即为config.properties

第五种, 读为流文件

InputStream inputStream = Resources.class.getResourceAsStream(“/mybatis.xml”);或者

InputStream resourceAsStream = Resources.class.getClassLoader().getResourceAsStream("a.py");

1

2

第六种, 读取路径

URL resource = Resources.class.getClassLoader().getResource("");

String path = resource.getPath();

底层如下:

public static URL getResource(String resourceName) {

ClassLoader loader = MoreObjects.firstNonNull(

Thread.currentThread().getContextClassLoader(),

Resources.class.getClassLoader());

URL url = loader.getResource(resourceName);

checkArgument(url != null, "resource %s not found.", resourceName);

return url;

}

————————————————

版权声明:本文为CSDN博主「世界,你好」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_41622739/article/details/97819146

来源:oschina

链接:https://my.oschina.net/u/2963604/blog/4302656

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用以下步骤来读取resources中的JSON文件: 1. 首先,确保将JSON文件放置在src/main/resources目录下,这是Java默认的资源文件夹。 2. 使用`ClassLoader`类的`getResourceAsStream`方法来获取JSON文件的输入流。代码示例如下: ``` InputStream inputStream = getClass().getClassLoader().getResourceAsStream("filename.json"); ``` 这里的`filename.json`是你要读取的JSON文件文件名。 3. 使用`BufferedReader`类来读取输入流中的内容。代码示例如下: ``` BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); ``` 4. 使用`StringBuilder`类来构建JSON文件的内容。代码示例如下: ``` StringBuilder jsonContent = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { jsonContent.append(line); } ``` 5. 最后,将读取到的JSON内容转换为标准的JSON对象。你可以使用任何适合的JSON库来完成此操作,例如`JSONObject`类或`Gson`库。 综上所述,以上是读取resources中的JSON文件的步骤。你可以根据你的具体需求进行适当的调整和扩展。<span class="em">1</span> #### 引用[.reference_title] - *1* [java怎么读取resources下面的JSON串文件](https://blog.csdn.net/laughing1997/article/details/103589602)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值