Rhs*_*Rhs 10 java netbeans path
我有以下代码来读取文本文件.
public static void main(String[] args)
{
try
{
Scanner in = new Scanner(new FileReader("input.txt"));
while(in.hasNext())
{
System.out.println(in.next());
}
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我的项目结构设置如下:
build/ directory contains class
dist/ directory contains the jar file
src/ directory contains source
input.txt the text file to read
Run Code Online (Sandbox Code Playgroud)
如果我把我的文本文件input.txt到一个名为目录test是相同的目录作为build,dist以及src什么应该进入的参数filereader,这样我仍然可以找到这个文件?
fvu*_*fvu 10
在Netbeans IDE中运行时,工作目录是项目的根目录,因此要回答您的问题"test/input.txt".
但请注意,虽然这对于测试代码非常好,但在最终(生产)代码中使用这样的相对路径可能会更棘手.在那些情况下,将文件作为资源包装在jar中并将其作为资源流打开可能是更好的解决方案,或者当然使用绝对路径.
如果您知道子目录的名称,请使用
Scannner in = new Scanner(new FileReader("test/input.txt"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42913 次 |
| 最近记录: |