从/src/main/resources/读取文件 [英] Read file from /src/main/resources/

查看:37
本文介绍了从/src/main/resources/读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个网络应用程序,但遇到了一个问题:我不知道如何用Java打开保存在资源文件夹中的文本文件:

I am trying to do a web application and have a problem: I don't know how to open a text file with Java that is saved in the resource folder:

 String relativeWebPath ="/src/main/resources/words.txt";  //Import der des Textdoumentes
 String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
 File f = new File(absoluteDiskPath);

(文件words.txt)

(The file words.txt)

正如您在图片中看到的,我正在尝试访问 words.txt,但它不起作用.有什么想法吗?

As you can see on the image I am trying to access words.txt but it isn't working. Any ideas?

推荐答案

试试这个.

InputStream is = getClass().getClassLoader()
                         .getResourceAsStream("/words.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));

这篇关于从/src/main/resources/读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆