質問でコンパイル1、SRC /メイン/ javaファイル
Mavenの構成は、コンパイルされませんSRC /メイン/ Javaでそれ以外の場合は設定ファイルを必要とされます。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.txt</include>
</includes>
</resource>
</resources>
2、ファイルを読む:のgetResourceを
//相对路径
//file.txt 在当前代码类的相对路径下去找
//如:D:/work/blog-test/target/classes/com/vim/test/file.txt
String path = this.getClass().getResource("file.txt").getPath();
//绝对路径
//file.txt 在当前 classpath 下已绝对路径去找
//如:D:/work/blog-test/target/classes/txt/file.txt
String path = this.getClass().getResource("/txt/file.txt").getPath();
3、ファイルをお読みください。ClassPathResourceの春
//此处全部是相对于 classpath 路径的
ClassPathResource resource = new ClassPathResource("templates/file-resource2.txt");
4、プロパティファイルを読み込む:クラスパスに基づきます
public void test001() throws Exception{
InputStream in = this.getClass().getResourceAsStream("/test.properties");
Properties properties = new Properties();
properties.load(in);
System.out.println(properties.getProperty("user"));
}
使用するResourceBundle:5、プロパティファイルを読み込み、
public void test001() throws Exception{
ResourceBundle resource = ResourceBundle.getBundle("test");
System.out.println(resource.getString("user"));
}
public void test001() throws Exception{
InputStream in = this.getClass().getResourceAsStream("/test.properties");
ResourceBundle resource = new PropertyResourceBundle(in);
System.out.println(resource.getString("user"));
}