从/ src / main / resources / [英] Read file from /src/main/resources/

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

问题描述

你好,我试图做一个Web应用程序,并有一个问题:
我不知道如何打开一个保存在资源文件夹中的Java文本文件:



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

(文件word.txt)



如图所示,我正在尝试访问word.txt,但是它不起作用。任何想法?

解决方案

尝试这个。

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


Hello 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);

(The file words.txt)

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

解决方案

Try this.

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

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

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