如何在eclipse中获取一个conf.txt路径 [英] how to get a conf.txt path in eclipse

查看:164
本文介绍了如何在eclipse中获取一个conf.txt路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 +\\\
;
}
reader.close();
} catch(Exception e){
throw new RuntimeException(e);
} finally {
if(file!= null){
try {
file.close();
} catch(IOException e){
//忽略关闭期间的问题
}
}
}
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();
}
}

}

我希望有人知道该怎么办



谢谢!

解决方案

我不知道,屏幕截图有一点点解释。如果您有任何问题,请通知我。



您的项目是这里的根文件夹,图像作为资源文件夹,您可以使用相对路径访问文件。

  //在根目录中查找文件 - > file.txt 
scan = new Scanner((new File(file.txt)));

//在给定的相对路径中寻找文件,即根 - >图像 - > file.txt
scan = new Scanner((new File(images / file.txt)));


I have an eclipse project and in one folder there is a text file "conf.txt". I can read and write the file when I use the path on my Computer. But I have to write my own folders there as well, not only the workspace folders. So know I want to commit the program for others, but then the path I put in the program won't work, because the program is running on a different computer. What I need is to be able to use the file with only the path in my workspace.

If I just put in the path, which is in the workspace it won't work.

This is how my class File looks like.

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();
      }
  }

}

I hope someone knows what to do.

Thanks!

解决方案

I am not sure but I attached the screen shot with little bit explanation. Let me know if you have any question.

Your project is root folder here and images as resources folder from where you can access the file using relative path.

// looks for file in root --> file.txt
scan = new Scanner((new File("file.txt")));

// looks for file in given relative path i.e. root--> images--> file.txt
scan = new Scanner((new File("images/file.txt"))); 

这篇关于如何在eclipse中获取一个conf.txt路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆