【发布时间】:2013-06-22 20:51:15
【问题描述】:
我有一个 Eclipse 项目,在一个文件夹中有一个文本文件“conf.txt”。当我使用计算机上的路径时,我可以读取和写入文件。但我也必须在那里编写自己的文件夹,而不仅仅是工作区文件夹。 所以知道我想为其他人提交程序,但是我在程序中输入的路径将不起作用,因为程序在另一台计算机上运行。 我需要的是能够使用我的工作区中只有路径的文件。
如果我只是输入路径,它在工作区中,它将不起作用。
这就是我的类文件的样子。
public class FileUtil {
public String readTextFile(String fileName) {
String returnValue = "";
FileReader file = null;
try {
file = new FileReader(fileName);
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null) {
returnValue += line + "\n";
}
reader.close();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
// Ignore issues during closing
}
}
}
return returnValue;
}
public void writeTextFile(String fileName, String s) throws IOException {
BufferedWriter output = new BufferedWriter(new FileWriter(fileName));
try {
output.write(s);
}
finally {
output.close();
}
}
}
我希望有人知道该怎么做。
谢谢!
【问题讨论】:
-
如果我对您的理解正确,您只想使用相对路径而不是完全限定的绝对路径?例如。 ./resources/conf.txt 而不是 C://Files/Resources/conf.txt
-
什么样的项目? Java 项目、动态 Web 项目等...
-
是的,我就是这个意思。那么我在 conf.txt 前面放什么?
-
Java 项目,抱歉,忘了说。
-
@Annika 您的
conf.txt文件在哪里?我的意思是它在您的项目根文件夹中还是在项目文件夹之外?可能会显示一些带有文件位置的代码 sn-p 将有助于解决您的问题。
标签: java eclipse path text-files workspace