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
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Jclass.main(Jclass.java:18)
最佳答案
因为只有 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 ();
关于java - 在java中读取.txt文件时出现ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467970/