FileScanner 对于前几行以整数与字符串连接的 Txt 文件没有下一行

问题描述 投票:0回答:1

我正在尝试使用Java中的文件扫描器来读取txt文件并使用文件中的解析信息填充链接列表。该文件以制表符分隔,需要获取“名称”和“控制台”。该文件的格式类似于:

10-Yard Fight (5 Screw Cartridge)   Nintendo Entertainment System [US]
3-D Worldrunner (5 Screw Cartridge) Nintendo Entertainment System [US]
720�    Nintendo Entertainment System [US]
8 Eyes  Nintendo Entertainment System [US]
Action 52   Nintendo Entertainment System [US]
"Addams Family, The"    Nintendo Entertainment System [US]
Adventure   Atari 2600 [NA]
"Adventures of Bayou Billy, The"    Nintendo Entertainment System [US]
"Adventures of Rad Gravity, The"    Nintendo Entertainment System [US]
Airwolf Nintendo Entertainment System [US]
Anticipation    Nintendo Entertainment System [US]
Armor Ambush (Black Label)  Atari 2600 [NA]

它永远不会有下一行并立即退出 while 循环。如果我将前四项移至 txt 文件的底部,例如:

Action 52   Nintendo Entertainment System [US]
"Addams Family, The"    Nintendo Entertainment System [US]
Adventure   Atari 2600 [NA]
"Adventures of Bayou Billy, The"    Nintendo Entertainment System [US]
"Adventures of Rad Gravity, The"    Nintendo Entertainment System [US]
Airwolf Nintendo Entertainment System [US]
Anticipation    Nintendo Entertainment System [US]
Armor Ambush (Black Label)  Atari 2600 [NA]
10-Yard Fight (5 Screw Cartridge)   Nintendo Entertainment System [US]
3-D Worldrunner (5 Screw Cartridge) Nintendo Entertainment System [US]
720�    Nintendo Entertainment System [US]
8 Eyes  Nintendo Entertainment System [US]

效果很好。我正在使用 hasNextLine()。请告诉我是否有明显的我忽略的事情。代码如下:

   try{
        File file = new File(fileName);
   
        Scanner fileScanner = new Scanner(file);

        int nameCol = 0;
        int consoleCol = 1;
        //FILESCANNER DOES NOT HAVE THE NEXT LINE
        // if(!(fileScanner.hasNextInt()||fileScanner.hasNextLine()))
        //     System.out.println("filescanner does not have next line");

        //won't work skips over while loop
        while(fileScanner.hasNextLine())
        {
             String line = fileScanner.nextLine();
            
             if(line!=""){
                     String[] items = line.split(DELIM);
                     if(items.length==COLS){
                       String name = items[nameCol];
                       String console = items[consoleCol];

                       VideoGame temp = new VideoGame(name,console);
                       games.add(temp);
                     }
             }
         }
         fileScanner.close();
    }catch(FileNotFoundException e){
        e.printStackTrace();
        System.exit(1);
    }

我尝试在 while 循环中使用 hasNextInt()||hasNextLine() 。我还知道系统识别出该文件存在。对于任何混乱的代码,我深表歉意,因为我是新手。再次注意,代码运行良好,底部有数字起始行。不幸的是,我不能简单地重写文件,必须使用原始文件的格式。

java file file-io filereader
1个回答
0
投票

用 Java 进行自己的 CSV 解析通常是一个糟糕的计划,因为它很容易出错,所以最好使用库。但是,如果您的文件很简单,那么您可以在练习时自己尝试一下。如果您有一个像这样的制表符分隔文件(不幸的是,该网站已将制表符转换为四个空格,因此我上面关于发布链接的评论):

“亚当斯一家”任天堂娱乐系统 [美国]
冒险雅达利 2600 [NA]
任天堂娱乐系统“Bayou Billy 的冒险”[美国]
任天堂娱乐系统“辐射重力历险记”[美国]
Airwolf 任天堂娱乐系统 [美国]
期待任天堂娱乐系统 [美国]
Armor Ambush(黑标)Atari 2600 [NA]

然后你可以使用你的

Scanner
来处理这个问题。*在你的bean中使用特殊的“valueOf”方法来处理CSV来简化问题,在这里
VideoGame::valueOfCsv
这样你就可以轻松地从文本创建它:

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;

public class VideoGameReader {
    public static void main(String[] args) throws Exception {
        try (Scanner s = new Scanner(Files.newBufferedReader(Path.of(args[0]))).useDelimiter("\t|\\R")) {
            while(s.hasNextLine()) {
                System.out.println(VideoGame.valueOfCsv(s.nextLine(), "\t"));
            }
        }
    }
}

上面的文件给出以下输出:

name="亚当斯一家",console=任天堂娱乐系统 [美国]
名称=冒险,控制台=Atari 2600 [NA]
name="Adventures of Bayou Billy, The",console=任天堂娱乐系统 [美国]
name="Radventures of Rad Gravity, The",console=任天堂娱乐系统 [美国]
名称=Airwolf,控制台=任天堂娱乐系统 [美国]
名称=预期,控制台=任天堂娱乐系统 [美国]
名称=Armor Ambush(黑标),控制台=Atari 2600 [NA]
  • (当然,您会注意到,如果您的 bean 正在处理 CSV,则更改
    Scanner
    的默认分隔符是多余的,但我为了说明目的而更改了它)
© www.soinside.com 2019 - 2024. All rights reserved.