[英]How to name a file from a folder in java?
I have a file named InputFile.txt in a resources folder. 我在资源文件夹中有一个名为InputFile.txt的文件。 My project structure is like this:
我的项目结构如下:
And I am trying to access the InputFile.txt in VirtualMemory.java class by like this: 我试图通过这样访问VirtualMemory.java类中的InputFile.txt:
String filename = ("./src/resources/InputFile.txt");
File file = new File(filename);
But the file is not being found. 但是找不到该文件。 How to resolve this problem?
如何解决这个问题?
Below code will help load a properties file from any where in the classpath. 下面的代码将帮助从类路径中的任何位置加载属性文件。
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
URL url = cl.getResource(CONF_PROPERTIES);
if (url == null) {
url = cl.getResource("/" + CONF_PROPERTIES);
}
if (url != null) {
try {
InputStream in = url.openStream();
props = new Properties();
props.load(in);
} catch (IOException e) {
// Log the exception
} finally {
// close opened resources
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.