admin管理员组文章数量:1195292
I'm trying to read a file called "words.txt", which sits in my src folder.
My code currently looks like this:
package hangman;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class GetHangmanWords {
public static String[] get() throws IOException {
InputStream in = GetHangmanWords.class.getClass().getClassLoader().getResourceAsStream("words.txt");
InputStreamReader inReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inReader);
String[] words = {};
ArrayList<String> lines = new ArrayList<String>();
while (reader.readLine() != null) {
lines.add(reader.readLine());
}
words = (String[]) lines.toArray();
return words;
}
}
But when I try to call this code like this:
package hangman;
import java.io.IOException;
public class Test {
public static void main (String [] args) throws IOException {
String[] words = GetHangmanWords.get();
}
}
I get:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.ClassLoader.getResourceAsStream(String)" because the return value of "java.lang.Class.getClassLoader()" is null
at hangman/hangman.GetHangmanWords.get(GetHangmanWords.java:11)
at hangman/hangman.Test.main(Test.java:7)
I don't understand enough about the mechanics at work here, since I'm fairly new to java, and I can't make sense of other explanations. If someone could help me, that would be much appreciated!
I'm trying to read a file called "words.txt", which sits in my src folder.
My code currently looks like this:
package hangman;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class GetHangmanWords {
public static String[] get() throws IOException {
InputStream in = GetHangmanWords.class.getClass().getClassLoader().getResourceAsStream("words.txt");
InputStreamReader inReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inReader);
String[] words = {};
ArrayList<String> lines = new ArrayList<String>();
while (reader.readLine() != null) {
lines.add(reader.readLine());
}
words = (String[]) lines.toArray();
return words;
}
}
But when I try to call this code like this:
package hangman;
import java.io.IOException;
public class Test {
public static void main (String [] args) throws IOException {
String[] words = GetHangmanWords.get();
}
}
I get:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.ClassLoader.getResourceAsStream(String)" because the return value of "java.lang.Class.getClassLoader()" is null
at hangman/hangman.GetHangmanWords.get(GetHangmanWords.java:11)
at hangman/hangman.Test.main(Test.java:7)
I don't understand enough about the mechanics at work here, since I'm fairly new to java, and I can't make sense of other explanations. If someone could help me, that would be much appreciated!
Share Improve this question edited Jan 23 at 13:34 Dante_05 asked Jan 23 at 7:59 Dante_05Dante_05 113 bronze badges 8 | Show 3 more comments2 Answers
Reset to default 1A resource lookup does not work with GetHangmanWords.class.getClass() ...
as that means you looking for a resource of java.lang.Class
rather than the class definition of GetHangmanWords
. Instead use:
InputStream in = GetHangmanWords.class.getResourceAsStream("words.txt")
You could save a lot of typing and use built-in calls of BufferedReader to retrieve a list or array of String:
public static List<String> get() throws IOException {
InputStream words = GetHangmanWords.class.getResourceAsStream("words.txt");
try(var reader = new BufferedReader(new InputStreamReader(words))) {
return reader.lines().toList();
}
}
If really wanting ArrayList use a different return
:
return reader.lines().collect(Collectors.toCollection(ArrayList::new));
I found a solution that worked! Here's my current code. I've forgone the conversion from an ArrayList to an Array, and am now working with an ArrayList on the other side:
package hangman;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class GetHangmanWords {
public static ArrayList<String> get() throws IOException {
InputStream in = GetHangmanWords.class.getResourceAsStream("words.txt");
InputStreamReader inReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inReader);
ArrayList<String> lines = new ArrayList<String>();
while (lines.contains(null) != true) {
lines.add(reader.readLine());
}
lines.removeLast();
return lines;
}
}
Thanks especially to @DuncG for getting me on the right path!
本文标签: javaHow to get the content of a file from the src folderStack Overflow
版权声明:本文标题:java - How to get the content of a file from the src folder? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738517333a2091160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
words = Files.readAllLines(Path.of(Argos[0]));
Be careful about the path you use. You need to know the current directory to get that right – g00se Commented Jan 23 at 8:19GetHangmanWords.class.getResourceAsStream("words.txt")
, assuming the resource is bundled with the same code – DuncG Commented Jan 23 at 8:53public static String[] get() throws IOException {String[] words = null;try (InputStream in = GetHangmanWords.class.getResourceAsStream("/words.txt")) {words = new Scanner(in).useDelimiter("\\R").tokens().toArray(String[]::new);}return words;}
– g00se Commented Jan 23 at 10:53