Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 跳过FileReader中的预定义行_Java_File_Comments - Fatal编程技术网

Java 跳过FileReader中的预定义行

Java 跳过FileReader中的预定义行,java,file,comments,Java,File,Comments,我一直在编写一个基于文本的RPG游戏,我正在尝试实现一个保存游戏的功能。一切都已编码并正常工作 它的工作原理是有一个名为slist的文件,其中包含savegame的名称和会话ID号。然后,每个savegame都有一个文件。程序扫描此文件以查看是否存在保存文件,然后从中确定操作 注意:我知道这可以简化很多,但我想自己学习一下 我遇到的问题是,我希望在使用FileReader读取文件时能够跳过行。这样用户就可以彼此共享文件,我可以在文件顶部为他们添加注释,见下文 private static Str

我一直在编写一个基于文本的RPG游戏,我正在尝试实现一个保存游戏的功能。一切都已编码并正常工作

它的工作原理是有一个名为slist的文件,其中包含savegame的名称和会话ID号。然后,每个savegame都有一个文件。程序扫描此文件以查看是否存在保存文件,然后从中确定操作

注意:我知道这可以简化很多,但我想自己学习一下

我遇到的问题是,我希望在使用FileReader读取文件时能够跳过行。这样用户就可以彼此共享文件,我可以在文件顶部为他们添加注释,见下文

private static String currentDir = new File("").getAbsolutePath();
private static File sessionList= new File(currentDir + "\\saves\\slist.dat"); //file that contains a list of all save files

private static void readSaveNames() throws FileNotFoundException {

Scanner saveNameReader = new Scanner(new FileReader(sessionList));

int idTemp;
String nameTemp;

while (saveNameReader.hasNext()) {

//   if line in file contains #, skip the line
nameTemp = saveNameReader.next();
idTemp = saveNameReader.nextInt();
saveNames.add(nameTemp);
sessionIDs.add(idTemp);
}
saveNameReader.close();
}
我尝试过使用Scanner.nextLine,但需要能够在文件中的任何位置插入某个字符,并使其跳过字符后面的行,请参见下文

private static String currentDir = new File("").getAbsolutePath();
private static File sessionList= new File(currentDir + "\\saves\\slist.dat"); //file that contains a list of all save files

private static void readSaveNames() throws FileNotFoundException {

Scanner saveNameReader = new Scanner(new FileReader(sessionList));

int idTemp;
String nameTemp;

while (saveNameReader.hasNext()) {

//   if line in file contains #, skip the line
nameTemp = saveNameReader.next();
idTemp = saveNameReader.nextInt();
saveNames.add(nameTemp);
sessionIDs.add(idTemp);
}
saveNameReader.close();
}
它引用的文件看起来像这样:

# ANY LINES WITH A # BEFORE THEM WILL BE IGNORED.
# To manually add additional save files,
# enter a new blank line and enter the
# SaveName and the SessionID.
# Example: ExampleGame 1234567890
GenericGame 1234567890
TestGame 0987654321
#skipreadingme 8284929322
JohnsGame 2718423422

有没有办法做到这一点,或者我必须去掉文件中的任何注释并使用for循环跳过前5行?

我的Java有点生疏,但是

while (saveNameReader.hasNext()) {

  nameTemp = saveNameReader.next();

  //   if line in file contains #, skip the line
  if (nameTemp.startsWith("#"))
  {
    saveNameReader.nextLine();
    continue;
  }

  idTemp = saveNameReader.nextInt();
  saveNames.add(nameTemp);
  sessionIDs.add(idTemp);
}

直到您到达最后一个会话ID 2718423422为止。它读取名称,但不读取ID号。2718423422对于Java int太大,因此引发Java.util.InputMismatchException错误。最大int值为2147483647。您将需要使用long。请参阅Java原语类型limitsDidn’t catch,我随机输入了10个数字。我将sessionID改为9个数字,这样它就不会再遇到这个错误了。