如何在 Java 中讀取檔案中的指定數量的元素?
要從檔案中讀取固定數量的元素,可以從檔案中讀取所需數量的資料元素並對它們進行處理,或者,將整個檔案讀入到一個集合或陣列中,並對每個 n 元素進行處理。
示例
以下 Java 程式一次讀取檔案中的 10 個單詞,並分別在一行中列印它們。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadingData {
public static void main(String args[]) throws FileNotFoundException {
//Creating an object of the File to read data
File file = new File("D://sampleData.txt");
//Instantiating the Scanner class
Scanner sc = new Scanner(file);
//Reading 10 words at a time
while(sc.hasNextLine()) {
StringBuffer sb = new StringBuffer();
for(int i=0; i<10; i++) {
sb.append(sc.next()+" ");
}
System.out.println(sb.toString());
}
}
}輸出
Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. 40 million readers read 100 million pages every month. Our content and resources are freely available and we prefer to keep it that way to encourage our readers acquire as many skills as they would like to. We don’t force our readers to sign up with us or submit their details either. No preconditions and no impediments. Simply Easy Learning!
示例
以下 Java 程式將檔案的內容讀入一個字串,並將其分割為一個單詞陣列,並對每 10 個元素列印陣列的內容。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadingData {
public static void main(String args[]) throws FileNotFoundException {
//Creating an object of the File to read data
File file = new File("D://sampleData.txt");
//Instantiating the Scanner class
Scanner sc = new Scanner(file);
String input;
StringBuffer sb = new StringBuffer();
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input+" ");
}
//Splitting the String to String array
String str[] = sb.toString().split(" ");
//Printing the contents of the array for every 10 words
int count = 0;
for(int i=0; i< str.length; i++) {
count++;
System.out.print(str[i]+" ");
if(count%10==0) {
System.out.println("");
}
}
}
}輸出
Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. 40 million readers read 100 million pages every month. Our content and resources are freely available and we prefer to keep it that way to encourage our readers acquire as many skills as they would like to. We don’t force our readers to sign up with us or submit their details either. No preconditions and no impediments. Simply Easy Learning!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP