如何从资源文件夹中正确获取文件
How to Properly Get a File from resources Folder
许多从资源文件夹读取文件的教程都使用 class 加载器。但是,使用该方法我无法解决静态警告问题,结果始终是空指针异常。
public class Test {
public static void main(String[] args) {
StringBuilder contentBuilder=new StringBuilder();
ClassLoader classLoader=Test.class.getClassLoader();
File file=new File(classLoader.getSystemResource("test.html").getFile());
try {
BufferedReader buffer=new BufferedReader(new FileReader(file));
String sCurrentLine="";
while ((sCurrentLine=buffer.readLine())!=null) {
contentBuilder.append(sCurrentLine);
}
buffer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
String content=contentBuilder.toString();
System.out.println("content="+content);
}
}
我的 IDE 在 "File" 行发出的警告是:
The static method getSystemResource(String) from the type ClassLoader should be accessed in a static way
我不知道如何消除警告,如果我只是忽略它和 运行 代码,我会得到一个空指针异常。为什么我会得到这个以及如何解决它? TIA.
或者您可以尝试一次全部阅读:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String content;
try {
content = new String(Files.readAllBytes(Paths.get(loader.getResource("test.html").toURI())), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
如果您使用 maven,请记得根据您的情况设置 resources(或者 Sources Root
或 Test Sources Root
)。
顺便说一下,您的解决方案使用 Test.class.getClassLoader();
就可以了。
Test.class.getClassLoader();
从 Class
' 方法 public ClassLoader getClassLoader()
获取对 ClassLoader
class 的引用 - 请参阅下面的 private final ClassLoader classLoader
。
由于您是从 class 的对象访问 ClassLoader
class,因此您不是以静态方式访问它。
来自 Class.java
、Java SE 1.7:
@CallerSensitive
public ClassLoader getClassLoader() {
ClassLoader cl = getClassLoader0();
if (cl == null)
return null;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
}
return cl;
}
// Package-private to allow ClassLoader access
ClassLoader getClassLoader0() { return classLoader; }
// Initialized in JVM not by private constructor
// This field is filtered from reflection access, i.e. getDeclaredField
// will throw NoSuchFieldException
private final ClassLoader classLoader;
为了以静态方式访问该方法,必须从 class 调用它,如果您想摆脱警告,它会将其声明为静态的:
ClassLoader.getSystemResource("test.html").getFile()
为了避免 NPE,test.html
文件应该在您的源文件夹下。
要回复您的评论,请使用 returns 不同于 URL
solves your problem - see Reading a resource file from within jar 的方法。
public class Test {
public static void main(String[] args) {
StringBuilder contentBuilder = new StringBuilder();
InputStream is = Test.class.getResourceAsStream("/test.html");
try {
String sCurrentLine;
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
while ((sCurrentLine = buffer.readLine())!=null)
contentBuilder.append(sCurrentLine);
buffer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
System.out.println("content=" + contentBuilder.toString());
}
}
许多从资源文件夹读取文件的教程都使用 class 加载器。但是,使用该方法我无法解决静态警告问题,结果始终是空指针异常。
public class Test {
public static void main(String[] args) {
StringBuilder contentBuilder=new StringBuilder();
ClassLoader classLoader=Test.class.getClassLoader();
File file=new File(classLoader.getSystemResource("test.html").getFile());
try {
BufferedReader buffer=new BufferedReader(new FileReader(file));
String sCurrentLine="";
while ((sCurrentLine=buffer.readLine())!=null) {
contentBuilder.append(sCurrentLine);
}
buffer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
String content=contentBuilder.toString();
System.out.println("content="+content);
}
}
我的 IDE 在 "File" 行发出的警告是:
The static method getSystemResource(String) from the type ClassLoader should be accessed in a static way
我不知道如何消除警告,如果我只是忽略它和 运行 代码,我会得到一个空指针异常。为什么我会得到这个以及如何解决它? TIA.
或者您可以尝试一次全部阅读:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String content;
try {
content = new String(Files.readAllBytes(Paths.get(loader.getResource("test.html").toURI())), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
如果您使用 maven,请记得根据您的情况设置 resources(或者 Sources Root
或 Test Sources Root
)。
顺便说一下,您的解决方案使用 Test.class.getClassLoader();
就可以了。
Test.class.getClassLoader();
从 Class
' 方法 public ClassLoader getClassLoader()
获取对 ClassLoader
class 的引用 - 请参阅下面的 private final ClassLoader classLoader
。
由于您是从 class 的对象访问 ClassLoader
class,因此您不是以静态方式访问它。
来自 Class.java
、Java SE 1.7:
@CallerSensitive
public ClassLoader getClassLoader() {
ClassLoader cl = getClassLoader0();
if (cl == null)
return null;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
}
return cl;
}
// Package-private to allow ClassLoader access
ClassLoader getClassLoader0() { return classLoader; }
// Initialized in JVM not by private constructor
// This field is filtered from reflection access, i.e. getDeclaredField
// will throw NoSuchFieldException
private final ClassLoader classLoader;
为了以静态方式访问该方法,必须从 class 调用它,如果您想摆脱警告,它会将其声明为静态的:
ClassLoader.getSystemResource("test.html").getFile()
为了避免 NPE,test.html
文件应该在您的源文件夹下。
要回复您的评论,请使用 returns 不同于 URL
solves your problem - see Reading a resource file from within jar 的方法。
public class Test {
public static void main(String[] args) {
StringBuilder contentBuilder = new StringBuilder();
InputStream is = Test.class.getResourceAsStream("/test.html");
try {
String sCurrentLine;
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
while ((sCurrentLine = buffer.readLine())!=null)
contentBuilder.append(sCurrentLine);
buffer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
System.out.println("content=" + contentBuilder.toString());
}
}