简体   繁体   English

具有多个分隔符的拆分和扫描仪

[英]Split and Scanner with multiple delimiters

I am attempting to create a Scanner with a delimiter that will parse through lines from a text file and then create arrays using this information and the split() method.我正在尝试创建一个带有分隔符的扫描仪,该分隔符将解析文本文件中的行,然后使用此信息和 split() 方法创建 arrays 。 However, I am unable to successfully stop the split() method from applying splitting the next line of the text file.但是,我无法成功阻止 split() 方法应用拆分文本文件的下一行。

public class Main {

public static void main(String[] args) throws FileNotFoundException {
fileReader("test.txt");
}

public static void fileReader(String filename) throws FileNotFoundException {
    Scanner in = new Scanner(new File(filename));
    in.useDelimiter(",");
    String[] test = in.next().split(":");
    String[] test2 = in.next().split(":");
    System.out.println(Arrays.toString(test));
    System.out.println("\n" + Arrays.toString(test2));
}

} }

test.txt测试.txt

A:B:C,ONE:TWO:THREE
ALPHA:BETA:CHARLIE,FOUR:FIVE:SIX

Output: Output:

[A, B, C]

[ONE, TWO, THREE
ALPHA, BETA, CHARLIE, FOUR, FIVE, SIX]

Expected Output:预期 Output:

[A, B, C] [ONE, TWO, THREE]

Note, I will eventually add a loop to parse through entire text file, creating Arrays using the other lines.注意,我最终将添加一个循环来解析整个文本文件,使用其他行创建 Arrays。

Replace代替

in.useDelimiter(",");

with

in.useDelimiter("[," + System.lineSeparator() + "]");

The regex, [,c] matches one of the characters inside the square bracket.正则表达式[,c]匹配方括号内的字符之一。 Thus, the split will happen on either , or c where c here refers to the line separator character returned by your operating system.因此,拆分将发生在c,其中c此处指的是操作系统返回的行分隔符。 Note that different operating system may return different line separators and therefore it's better to use System.lineSeparator() instead of specifying the line separator characters yourself.请注意,不同的操作系统可能会返回不同的行分隔符,因此最好使用System.lineSeparator()而不是自己指定行分隔符。 If you wish to specify the line separator characters yourself, you can specify [,\n\r] but I strongly recommend you use System.lineSeparator() instead of [\n\r] .如果您想自己指定行分隔符,您可以指定[,\n\r]但我强烈建议您使用System.lineSeparator()而不是[\n\r]

Well, as far as I can tell, you're setting the delimiter to "," .好吧,据我所知,您将分隔符设置为"," If you instead want to break at new lines, you should set it to "\n" instead.如果您想换行,则应将其设置为"\n" If you do want to use commas, then you should maybe add those to the end of the line(s).如果您确实想使用逗号,那么您应该将它们添加到行尾。

If, however, your goal is to split the file at both commas and new lines, then you should consider using a Reader and splitting the strings yourself when you encounter one of the two.但是,如果您的目标是在逗号和换行处拆分文件,那么您应该考虑使用 Reader 并在遇到两者之一时自己拆分字符串。

public static void fileReader(String filename) throws FileNotFoundException, IOException {
    Reader reader = new FileReader(new File(filename));
    StringBuilder sb = new StringBuilder();
    int c;

    while ((c = reader.read()) > 0) {
        if (c == ',' || c == '\n') {
            String[] s = sb.toString().split(":");
            sb.setLength(0);
            // Do something with the separated strings
        } else sb.append((char) c);
    }

    reader.close();
    // ...
}

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

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