How to read certain number of elements from a file in Java?


To read a fixed number of elements from a file you can either read a required number of data elements from the file and process them or, read the entire file into a collection or an array and process it for every n element.

Example

Following Java program, reads the contents of a file 10 words at a time and prints them in a separate line.

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());
      }
   }
}

Output

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!

Example

The following Java program reads the contents of the file into a string and splits it as an array of words and prints the contents of the array for every 10 elements.

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("");
         }
      }
   }
}

Output

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!
raja
Published on 11-Sep-2019 17:29:07
Advertisements