【问题标题】:File location for reading the file读取文件的文件位置
【发布时间】:2011-04-26 14:57:42
【问题描述】:

我需要在属性文件中设置一个文件位置,以便稍后在类文件中使用它。例如我有,

anamelistfile = /rsrs/anamelist.txt

这是直接在上下文下的文件夹 config 中的文件 propss.properties 中设置的。与“rsrs”文件夹相同。

在我的类文件中,我需要获取此文件位置。我使用以下代码来获取它,并在 fileloc 对象中获得了所需的值。

fileloc=Props.getproperty(anamelistfile )
//result was fileloc="/rsrs/anamelist.txt"

我使用以下代码来读取文件。但是一旦创建了 File 对象,它会将文件路径视为“\rsrs\anamelist.txt”,并且我得到了一个找不到文件的异常。

File listFile = new File(fileloc);
BufferedReader input = new BufferedReader(new FileReader(listFile));

错误信息 Exception: Stack Trace for:java.io.FileNotFoundException: \rsrs\anamelist.txt(系统找不到指定的路径)

有人可以帮我解决我在这里犯的错误吗?此外,我的开发环境是 windows,prod 是在 Unix 上,所以我需要解决方案来处理两者。提前致谢

【问题讨论】:

    标签: file file-io filepath


    【解决方案1】:

    试试……

    InputStream fIn = Thread.currentThread().getContextClassLoader().getResourceAsStream("/rsrs/anamelist.txt"); BufferedReader input = new BufferedReader(new InputStreamReader(fIn));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多