java写文件路径

编程入门 行业动态 更新时间:2024-10-09 10:31:55

java写文件<a href=https://www.elefans.com/category/jswz/34/1771438.html style=路径"/>

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写文件路径

本文发布于:2024-02-28 02:37:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1767665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   文件   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!