读取制表符分隔的文本文件 java [英] reading tab delimited textfile java

查看:75
本文介绍了读取制表符分隔的文本文件 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());
    }

这篇关于读取制表符分隔的文本文件 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆