【发布时间】:2016-10-20 04:55:48
【问题描述】:
我想写入我的文件 dir.txt。
它可以在 Eclipse 中运行,但是当我将项目导出为可运行的 JAR 文件时,PrintWriter 找不到该文件 (FileNotFoundException)。
我用这个:
public static final String DIR_PATH = "temp/dir.txt";
PrintWriter writer = new PrintWriter(new File(DIR_PATH));
我在同一个文件上使用了BufferedReader,但它也不起作用,但我修复了它这样做
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(DIR_PATH)));
我可以用PrintWriter 做同样的事情吗?
我在包资源管理器中的项目
【问题讨论】:
-
试试
/temp/dir.txt -
@YassinHajaj 如果我这样做,我的 BufferedReader 现在无法在 Eclipse 中工作
-
YourClass.class.getResource("/your_root_package/temp/dir.txt");
-
我注意到我的 PrintWriter 甚至可以在 Eclipse 中工作...我必须使用的路径是 src/temp/dir.txt,这就是为什么它不能在 .jar 中工作,因为没有/src 导出时。
-
@manikantgautam "构造函数 PrintWriter(URL) 未定义"
标签: java eclipse file jar printwriter