如何在 Java 中从文件中读取一定数量的元素?

java 8object oriented programmingprogramming更新于 2025/6/26 22:52:17

要从文件中读取固定数量的元素,您可以从文件中读取所需数量的数据元素并进行处理,或者将整个文件读入集合或数组中,然后对每个元素进行处理。

示例

以下 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 {
      //创建 File 对象以读取数据
      File file = new File("D://sampleData.txt");
      //实例化 Scanner 类
      Scanner sc = new Scanner(file);
      //一次读取 10 个单词
      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 {
      //创建 File 对象以读取数据
      File file = new File("D://sampleData.txt");
      //实例化 Scanner 类
      Scanner sc = new Scanner(file);
      String input;
      StringBuffer sb = new StringBuffer();
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input+" ");
      }
      //将字符串拆分为字符串数组
      String str[] = sb.toString().split(" ");
      //每 10 个单词打印一次数组内容
      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!

相关文章