【问题标题】:try catch cannot read filetry catch 无法读取文件
【发布时间】:2014-02-07 23:32:29
【问题描述】:

我正在运行此方法,但由于某种原因我无法找到数据记录,它一直在运行。有人可以让我知道我在哪里搞砸了。我的 data.txt 文件与 java 文件位于同一目录中。

public static Games[] gamesRecord(){
    Games[] game = new Games[50];
    try{
        Scanner dataFile = new Scanner(new File("data.txt"));        
        for(int i = 0; i > 50; i++){
            game[i].title = dataFile.next();
            game[i].releaseDate = dataFile.nextInt();
            game[i].redistributions = dataFile.nextInt();
            game[i].platformRelease = dataFile.next();
        }
    }catch(FileNotFoundException e){
        System.out.println("File data.txt was not found");
        System.out.println("or could not be opened.");
        System.exit(0);
    }
    return game;
}

【问题讨论】:

  • 确保 data.txt 实际上是 data.txt,而不是 data.txt.txt。我知道很多人都遇到过这种情况。
  • 如果您打印出异常堆栈跟踪会有所帮助...
  • 首先(虽然与手头的问题没有直接关系)将i > 50更改为i < 50
  • 名字设置错误,是data3,改了还是一样的问题,可惜我没有得到堆栈跟踪,只是上面放的异常
  • 在您的catch 中执行e.printStackTrace();

标签: java file find try-catch


【解决方案1】:

通过放置文件的完整路径来解决问题

【讨论】:

  • @mikeaworski 我删除了我的评论,但我仍然认为这应该更详细,以提高其他有相同问题的用户的可用性。
猜你喜欢
  • 2013-03-07
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-07
  • 2013-11-08
  • 2011-12-25
相关资源
最近更新 更多