路径"/>
java写文件路径
一. 相对路径
通过配置文件的方式配置文件路径
项目路径下创建 application.properties 配置文件
application.properties为键值对格式的配置文件 key=value
key和value的都是String类型的字符串
application.properties
#定义了一个test标识,表示test文件夹的abc文件
test=\\test\\abc
应用
public class App {
public static void main(String[] args) {
Properties pro = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(("application.properties")));
pro.load(in);
File file=new File(System.getProperty("user.dir")+pro.getProperty("test"));
System.out.println(file.getAbsolutePath());
BufferedReader br=new BufferedReader(new FileReader(file));
System.out.println(br.readLine());
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上述代码中通过 java.util.Properties 获取配置文件中的值
1.字符流的读取配置文件
InputStream in = new BufferedInputStream(new FileInputStream(("application.properties")));
2.创建Properties对象并加载配置文件内容
Properties pro = new Properties();
pro.load(in);
3.通过key获取value
pro.getProperty("test")
获取用户当前工作路径
System.getProperty("user.dir")
二. 绝对路径
通过配置文件的方式配置路径
与配置相对路径的方法相同
在配置文件中 value 的值为绝对路径
应用
File file=new File(pro.getProperty("test"));
更多推荐
java写文件路径
发布评论