IDEA 相对路径读取项目中的文件

时间:2019-10-05 11:53:01   收藏:0   阅读:1679

技术分享图片

1、目标:想要读取该项目中的resources下的prop文件夹中的text.txt文件

2、实现方法:

// 读文件
String path = this.getClass().getClassLoader().getResource("./prop/text.txt").getPath();
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);

String str = null;
while((str = br.readLine()) != null) {
    System.out.println(str);
}

// 关闭流
br.close();
fr.close();

 

参考:

1)javaweb(实用)-IDEA下resources目录下txt文件读取写入引发的项目后台路径问题总结

原文:https://www.cnblogs.com/GjqDream/p/11624190.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!