本文介紹了讀取制表符分隔的文本文件 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!