阅读制表符分隔的文本文件的Java [英] reading tab delimited textfile java
本文介绍了阅读制表符分隔的文本文件的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
10
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
ddd ddd ddd
我有一个文本文件,我试图用制表符分隔符读取。每当我读取文件时,都会在10之后出现arrayindexoutofbound错误。我在网上搜索并发现我必须在\t后面添加-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 \t 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("\t");
array.add(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屋!
查看全文