简体   繁体   English

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

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

Hello it is my code to read from file 您好,这是我的文件读取代码

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;
}

Example file looks like this: 示例文件如下所示:

Pop
Test|Test|2010
Test1|Test1|2001

Gives me error 给我错误

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

Why my line.split doesn't split when it finds "|"? 为什么当我的line.split找到“ |”时不拆分? I guess it splits tes, any tips? 我猜它分裂了,有什么提示吗?

The pipe character "|" 管道字符“ |” is one of the meta characters that carries a special meaning while performing the match. 是执行比赛时带有特殊含义的元字符之一。

This page gives you the complete lists of these special characters and their meanings. 页面为您提供了这些特殊字符及其含义的完整列表。

So, in your program, modify the following line, 因此,在您的程序中,修改以下行,

String delimiter = "|";

to

String delimiter = "\\|";

This will give you the result that you want. 这将为您提供所需的结果。

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

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