加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

NetBeans文本文件的Java项目路径

发布时间:2020-12-14 05:02:22 所属栏目:Java 来源:网络整理
导读:我有以下代码来读取一个文本文件. 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.getLogge
我有以下代码来读取一个文本文件.
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);
}
}

我的项目结构设置如下:

build/ directory contains class
dist/  directory contains the jar file 
src/ directory contains source
input.txt the text file to read

如果我将textfile input.txt放入一个名为test的目录中,该目录与build,dist和src位于同一个目录,那么应该进入filereader的参数,以便我仍然可以找到这个文件?

解决方法

当运行在Netbeans IDE中时,工作目录是项目的根目录,所以要回答你的问题“test / input.txt”.

但是请注意,尽管测试代码完全正确,但在最终(生产)代码中使用相对路径的工作可能更为棘手.在将文件作为资源包装在jar中并将其作为资源打开的情况下,可能是更好的解决方案,或者当然使用绝对路径.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读