- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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!
- Related Articles
- How to read the data from a file in Java?
- How to read a 2d array from a file in java?
- How to read data from .csv file in Java?
- How to read integers from a file using BufferedReader in Java?
- How to read a number of characters from a text file using Python?
- How to read data in from a file to String using java?
- Java program to delete certain text from a file
- How to read the data from a properties file in Java?\n
- How to read the data from a CSV file in Java?\n
- How to read/write data from/to .properties file in Java?
- How to read data from one file and print to another file in Java?
- How to remove certain number elements from an array in JavaScript
- How to read a text file from resources in Kotlin?
- How to read a .txt file with RandomAccessFile in Java?
- How to read a file from assets on android?

Advertisements