繁体   English   中英

从文件读取数组。 (Java)的

[英]Reading an array from file. (java)

您好,这是我的文件读取代码

case 11: {
    String line;
    String temp[];
    System.out.println("Podaj nazwę pliku z jakiego odczytać playlistę.");
    nazwa11 = odczyt.next();
    try {

        FileReader fileReader = new FileReader(nazwa11);

        BufferedReader bufferedReader = new BufferedReader(fileReader);
        playlists.add(new Playlist(bufferedReader.readLine()));
        x++;
        while((line = bufferedReader.readLine())!=null){
            String delimiter = "|";
            temp = line.split(delimiter);
            int rok;
            rok = Integer.parseInt(temp[2]);
            playlists.get(x).dodajUtwor(temp[0], temp[1], rok);


        }


        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Nie znaleziono pliku: '" + nazwa11 + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + nazwa11 + "'");
    }
    break;
}

示例文件如下所示:

Pop
Test|Test|2010
Test1|Test1|2001

给我错误

Exception in thread "main" java.lang.NumberFormatException: For input string: "s"

为什么当我的line.split找到“ |”时不拆分? 我猜它分裂了,有什么提示吗?

管道字符“ |” 是执行比赛时带有特殊含义的元字符之一。

页面为您提供了这些特殊字符及其含义的完整列表。

因此,在您的程序中,修改以下行,

String delimiter = "|";

String delimiter = "\\|";

这将为您提供所需的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM