将文件从src文件夹加载到阅读器中

我想知道如何从src文件夹lol.txt文件lol.txt加载到我的close方法中。 到目前为止的代码:

  public void close() throws IOException { boolean loadFromClasspath = true; String fileName = "..."; // provide an absolute path here to be sure that file is found BufferedReader reader = null; try { if (loadFromClasspath) { // loading from classpath // see the link above for more options InputStream in = getClass().getClassLoader().getResourceAsStream("lol.txt"); reader = new BufferedReader(new InputStreamReader(in)); } else { // load from file system reader = new BufferedReader(new FileReader(new File(fileName))); } String line = null; while ( (line = reader.readLine()) != null) { // do something with the line here System.out.println("Line read: " + line); } } catch (IOException e) { JOptionPane.showMessageDialog(null,e.getMessage()+" for lol.txt","File Error",JOptionPane.ERROR_MESSAGE); } finally { if (reader != null) { reader.close(); } } } 

启动时控制台错误输出:

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.io.Reader.(Unknown Source) at java.io.InputStreamReader.(Unknown Source) at main.main.close(main.java:191) at main.main$1.windowClosing(main.java:24) at java.awt.Window.processWindowEvent(Unknown Source) at javax.swing.JFrame.processWindowEvent(Unknown Source) at java.awt.Window.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 

如果您想从jar文件(即从类路径)加载文件,请参阅此答案以获取有关如何获取InputStream更多选项。 在下面的代码中,我遗漏了exception处理并删除了Random相关代码。

 public void close() { boolean loadFromClasspath = true; String fileName = "..."; // provide an absolute path here to be sure that file is found BufferedReader reader = null; try { if (loadFromClasspath) { // loading from classpath // see the link above for more options InputStream in = getClass().getClassLoader().getResourceAsStream("absolute/path/to/file/inside/jar/lol.txt"); reader = new BufferedReader(new InputStreamReader(in)); } else { // load from file system reader = new BufferedReader(new FileReader(new File(fileName))); } String line = null; while ( (line = reader.readLine()) != null) { // do something with the line here System.out.println("Line read: " + line); } } catch (IOException e) { JOptionPane.showMessageDialog(null,e.getMessage()+" for lol.txt","File Error",JOptionPane.ERROR_MESSAGE); } finally { if (reader != null) { reader.close(); } } } 

编辑:您似乎正在对文件夹结构执行错误操作,或者您使用了错误的包/文件名。 只是要清楚。 目前,您似乎在main包下面有一个名为main的类。 您的文件夹结构应该是这样的:

 + src/ + main/ main.java lol.txt 

当你编译时,你的lol.txt文件(顺便说一句,那些是小写的L而不是数字1对吗?)应该复制到/bin/main/文件夹下

如果是这种情况,那么使用如下代码: InputStream in = getClass().getClassLoader().getResourceAsStream("main/lol.txt");

如果您的文件夹结构不同,请相应更改

如果要从类路径获取文件(资源)的InputStream ,可以执行以下操作

 InputStream in = this.getClass().getResourceAsStream("lol.txt"); 

假设名为lol.txt的资源与getClass()表示并返回的类位于同一个包中。

如果资源不在同一个包中,则可以在路径前加上/ ,告诉方法查看类路径的根。

 InputStream in = this.getClass().getResourceAsStream("/lol.txt"); // or /some/resource/path/lol.txt for some other path starting at root of classpath 

如果您尝试从static方法访问资源,则无法访问this 。 你需要使用

 YourClass.class.getResourceAsStream("/lol.txt"); 

在这里阅读javadoc。