无法从资源文件夹中读取文件
Cannot read files from resources folder
最终编辑:现在代码如下所示
InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
String masterKey = null;
Scanner scanner = new Scanner(is);
masterKey = scanner.nextLine();
System.out.println("the master key is " + masterKey);
原版post
我在从 resources
文件夹读取 txt 文件时遇到问题。
项目结构如下:
projStructure
当我调用下面的代码时
System.out.println(getClass().getClassLoader().getResource("static/master-key.txt").getPath());
File mkFile = new File(getClass().getClassLoader().getResource("static/master-key.txt").getPath());
这会发生什么
/D:/Dropbox/Coding/Intellij%20IDEA/TishenkoKPO/target/classes/static/master-key.txt
java.io.FileNotFoundException:
D:\Dropbox\Coding\Intellij%20IDEA\TishenkoKPO\target\classes\static\master-key.txt (System cannot find the specified path)
我在谷歌上搜索了很多,但不知道为什么会这样
编辑 1:
根据社区建议重建的部分代码(将文件作为资源使用)
InputStream is = getClass().getResourceAsStream("static/master-key.txt");
String masterKey = null;
Scanner scanner = new Scanner(is);
masterKey = scanner.nextLine();
System.out.println("the master key is " + masterKey); //successfuly outputs the first line if exists
编辑 2: 资源路径应以 /
开头
InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
试试这个
getResource("resources\static\master-key.txt").getPath());
如果您定位路径,请使用“\”而不是“/”。
你可以试试
YourClass.class.getResourceAsStream("resources\static\master-key.txt");
或
YourClass.class.getResourceAsStream("static\master-key.txt");
InputStream is = getClass().getResourceAsStream("static/master-key.txt");
这是在搜索与调用它的 class 的包/目录相关的资源。很可能确实可以找到资源 相对于应用程序结构的根。 为此,添加前导 /
,如下所示。
InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
最终编辑:现在代码如下所示
InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
String masterKey = null;
Scanner scanner = new Scanner(is);
masterKey = scanner.nextLine();
System.out.println("the master key is " + masterKey);
原版post
我在从 resources
文件夹读取 txt 文件时遇到问题。
项目结构如下:
projStructure
当我调用下面的代码时
System.out.println(getClass().getClassLoader().getResource("static/master-key.txt").getPath());
File mkFile = new File(getClass().getClassLoader().getResource("static/master-key.txt").getPath());
这会发生什么
/D:/Dropbox/Coding/Intellij%20IDEA/TishenkoKPO/target/classes/static/master-key.txt
java.io.FileNotFoundException:
D:\Dropbox\Coding\Intellij%20IDEA\TishenkoKPO\target\classes\static\master-key.txt (System cannot find the specified path)
我在谷歌上搜索了很多,但不知道为什么会这样
编辑 1: 根据社区建议重建的部分代码(将文件作为资源使用)
InputStream is = getClass().getResourceAsStream("static/master-key.txt");
String masterKey = null;
Scanner scanner = new Scanner(is);
masterKey = scanner.nextLine();
System.out.println("the master key is " + masterKey); //successfuly outputs the first line if exists
编辑 2: 资源路径应以 /
InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
试试这个
getResource("resources\static\master-key.txt").getPath());
如果您定位路径,请使用“\”而不是“/”。 你可以试试
YourClass.class.getResourceAsStream("resources\static\master-key.txt");
或
YourClass.class.getResourceAsStream("static\master-key.txt");
InputStream is = getClass().getResourceAsStream("static/master-key.txt");
这是在搜索与调用它的 class 的包/目录相关的资源。很可能确实可以找到资源 相对于应用程序结构的根。 为此,添加前导 /
,如下所示。
InputStream is = getClass().getResourceAsStream("/static/master-key.txt");