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"));

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值