idea中获取项目中文件相对路径的方法

idea中获取项目中文件相对路径的方法,第1张

想要读取该项目中的resources下的prop文件夹中的渣扮text.txt文件。

// 读文件String path

this.getClass().getClassLoader().getResource("./prop/text.txt").getPath()

FileReader fr = new FileReader(path)

BufferedReader br = new BufferedReader(fr)

String str = nullwhile((str = br.readLine()) != null) {

System.out.println(str)

}// 关闭流br.close()

fr.close()

读文如销灶件:

public void load(String path) {

BufferedReader br = null

try {

br = new BufferedReader(new FileReader(path))

String line = ""

while ((line = br.readLine()) != null) {

m_tbl.put(Integer.parseInt(line), true)

}

br.close()

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (NumberFormatException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

}

}

扩展资料:

解决斗纳相对路径找不到的问题:

1、采用绝对路径;

2、还是使用相对路径,这时用类加载器加载文件路径。代码如下:

public void load(String path) {

BufferedReader br = null

try {

InputStream in = SetTable.class.getClassLoader().getResourceAsStream(path)

br = new BufferedReader(new InputStreamReader(in, "UTF-8"))

String line = ""

while ((line = br.readLine()) != null) {

m_tbl.put(Integer.parseInt(line), true)

}

br.close()

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (NumberFormatException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

}

}

在idea的快捷方式点右键,打开文件所在位置,进宏胡入idea安装位置的bin目录。

如果使用IDEA,就需要做则信很多配置的相关工作,让它越来越符合你的个人习惯,使蔽盯拦用起来得心应手,这些配置信息,都保存在C盘。

找到你IDEA的安装目录,进入bin目录,去掉下面这两行路径前的注释,修改路径为你想存放的路径。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12135811.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存