读取制表符分隔的文本文件 java

2022-01-09 00:00:00 file text delimiter tabs java
10
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
ddd ddd ddd

我有一个文本文件,我试图用制表符分隔符读取.每当我读取文件时,我在 10 之后得到一个 arrayindexoutofbound 错误.我在网上搜索,发现我必须在 后面添加一个 -1,但我仍然得到同样的错误.

I have a textfile that Im trying to read with tab delimiters. whenever i read the file, i get an arrayindexoutofbound error after the 10. i search online and found that i have to add a -1 behind the but i still get the same error.

 try{
        Scanner scan = new Scanner(new File("1.txt"));
        String line="";
        int readline = Integer.parseInt(scan.nextLine());//

        while (scan.hasNextLine())
        {
            line = scan.nextLine();

            if(line.equals("ccc"))
            {  
                break;
            }
        String[] split=line.split("	");

            array.add(split);
        } 

推荐答案

如果你使用Scanner这里不需要split,你可以使用next()这里如下

If you are use Scanner here no need to split, you can use next() here as follows

    Scanner sc=new Scanner(new FileReader("D:\test.txt"));
    while (sc.hasNextLine()){
        System.out.println(sc.next());
    }

相关文章