2016-12-14 52 views
-1

所以我有一個掃描儀,通過使用while(file.hasNext()),許多行的文本文件中讀取,但是,它到達文本文件的末尾後,我如何使它,以便我可以開始讀取再次開始一個單獨的while循環?如何從頭再次開始從文本中讀取行?

+0

檢查這個答案,probaly它會幫助你 http://stackoverflow.com/a/11665098/4060470 –

+0

創建的新實例用此文件掃描儀並再次讀取文本文件。根據行數,在第一次讀取之後存儲信息並重新使用它會更有效。 – davidxxx

+1

[重置.nextLine()掃描儀](http://stackoverflow.com/questions/13991494/resetting-a-nextline-scanner)可能的重複 – choasia

回答

0

如果你想讀取多個文件(例如,「text1.txt,text2.txt,text3.txt..etc」),這是你可以做什麼:

實現你的文件的方法中讀如:

public void readFile(String filename){ 
    //all the code for file reading goes here 
} 

String[] filesToRead = new String[]{"text1.txt", "text2.txt", "text3.txt"}; 
for(int x=0; x<filesToRead.length; x++) //iterate through the file names 
    readFile(filesToRead[x]);    //repeatedly invoked to read various files 

Certianly,你還可以保存文件名中的另一個文本文件,並從那裏剛讀。這樣,當您想要更改要讀取的文件列表時,甚至不必重新編譯程序。

例子:

FilesToRead.txt

text1.txt 
text2.txt 
text3.txt 
相關問題