在java中读取.txt文件时获取ArrayIndexOutOfBoundsException

时间:2016-04-07 06:09:28

标签: java

try (BufferedReader br = new BufferedReader(new FileReader("/home/modi/Desktop/hbaseoff/mobiledata.txt")))
        {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                String[] values = sCurrentLine.split("\t");
System.out.println(values[0]+"-"+values[1]+"\t"+values[2]+"\t"+values[3]+"\t"+values[4]);
}

        } catch (IOException e) {
            e.printStackTrace();
        } 

输出:

20150320-9876543217 16  45  22
20150320-8876543218 45  11  13
20150320-8876543219 49  15  16
20150321-9876543210 16  45  22
20150321-9876543211 45  11  13
  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:1     在Jclass.main(Jclass.java:18)

1 个答案:

答案 0 :(得分:2)

因为只有4个制表符分隔值

System.out.println(values[0]+"-"+values[1]+"\t"+values[2]+"\t"+values[3]);

就个人而言,我会把它放在循环中

for (String val : values) {
   System.out.print (val + "\t");
}
System.out.println ();