在Java程序中加载外部文件有多中方式,每种方式也存在区别,本文将理清这些加载方式之间的区别。java
package org.xialei.example.resource; import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { File file = new File("app.properties"); System.out.println(file.getAbsolutePath()); } }
常见的读取方式,使用该方式读取文件时规则以下:segmentfault
若是传入的是绝对路径,则以系统根目录做为绝对路径的起点。若是传入的是相对路径,则以当前工做目录做为起点。app
本例中,运行java
命令的目录即为工做目录,app.properties从工做目录开始查找。spa
package org.xialei.example.resource; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Main { public static void main(String[] args) throws IOException { try (InputStream is = Main.class.getResourceAsStream("app.properties")) { Properties properties = new Properties(); properties.load(is); System.out.println(properties.getProperty("name")); } } }
使用该方式读取文件时规则以下:code
若是传入的是相对路径,则以当前class所在的包做为起点。若是传入的是绝对路径,则以classpath的根目录为起点。get
Main.class.getResourceAsStream("app.properties")
会读取/org/xialei/example/resource/app.properties
文件。Main.class.getResourceAsStream("/app.properties")
会读取"classpath:/app.properties"文件package org.xialei.example.resource; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Main { public static void main(String[] args) throws IOException { try (InputStream is = Main.class.getClassLoader().getResourceAsStream("org/xialei/example/resource/app.properties")) { Properties properties = new Properties(); properties.load(is); System.out.println(properties.getProperty("name")); } } }
使用该方式时规则以下:it
使用classpath根目录做为起点。
本例中,org/xialei/example/resource/app.properties
就是从classpath根目录进行查找的。io