如何在 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!

更新日期:2019-09-11

677 次浏览

开启您的职业生涯

完成课程获得认证

开始
广告