Java Reading From a File Line by Line
Prior to Java 7, reading a text file into an ArrayList involves a lot of boilerplate coding, equally you need to read the file line by line and insert each line into an ArrayList, but from Java 7 onward, you can use the utility method Files.readAllLines() to read all lines of a text file into a Listing. This method returns a List of String that contains all lines of files. Later you can convert this List to ArrayList, LinkedList, or whatever list you want to. Btw, this the fourth article in the serial of reading a text file in Java.
In the earlier parts, y'all take learned how to read a file using Scanner and BufferedReader (1). Then, reading the whole file as String (2) and finally reading a text file into an array (3 ). This plan is non very dissimilar from those in terms of fundamentals.
We are still going to use theread() method for Java 6 solution and will read all text until this method returns -1 which signals the end of the file.
Reading text file into ArrayList in Java - BufferedReader Example
If you know how to read a file line past line, either past using Scanner or by using BufferedReader then reading a text file into ArrayList is non difficult for you. All y'all need to practice is read each line and store that into ArrayList, equally shown in the following example:
BufferedReader bufReader = new BufferedReader(new FileReader("file.txt")); ArrayList<Cord> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line ! = null) { listOfLines.add together(line); line = bufReader.readLine(); } bufReader.shut();
Just think to close the BufferedReader once you are washed to forbid resource leak, as y'all don't have a attempt-with-resource argument in Java six.
Reading text file into List in Coffee - Files.readAllLines Instance
In Java 7, yous don't need to write code to read and store into ArrayList, but call the Files.readAllLines() method and this will return you a listing of String, where each chemical element is the corresponding line from the line. Since Listing is an ordered drove the order of lines in a file is preserved in the list. You tin later convert this List to ArrayList or whatever other implementation.
here is sample code to read text file into List in JDK seven:
public static List<String> readFileIntoList(String file) { List<Cord> lines = Collections.emptyList(); try { lines = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8); } catch (IOException due east) { // TODO Automobile-generated catch block east.printStackTrace(); } return lines; }
The readAllLines() method accepts a CharSet, you can utilize a pre-divers grapheme set due east.g. StandardCharsets.UTF_8 or StandardCharsets.UTF_16. You can likewise see these free Java Courses to larn more nigh new file utility classes introduced in Coffee seven and eight.
Java Program to read text file into ArrayList
Here is the complete Java program to demonstrate both methods to read a text file into ArrayList. This program first teaches y'all how to practise this in JDK 7 or Java eight using theFiles.readAllLines() method and after using BufferedReader and ArrayList in Java 6 and lower version.
You lot can compile and run this program from the command prompt or if you want to run in Eclipse, merely copy and paste in Eclipse Coffee project. The Eclipse IDE will automatically create a source file for you.
Then simply right-click and Run as Java Program. Make sure you take file.txt in your classpath. Since I take given the relative path hither, make sure yous put that file inside the Eclipse project directory.
Reading text file into ArrayList in Coffee
import java.io.BufferedReader; import coffee.io.FileReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import coffee.nio.file.Paths; import coffee.util.ArrayList; import java.util.Collections; import java.util.List; /* * Java Program read a text file into ArrayList in Coffee six * and Java 8. */ public form ReadFileIntoArrayList { public static void main(String[] args) throws Exception { // reading text file into List in Java seven List<String> lines = Collections.emptyList(); endeavor { lines = Files.readAllLines(Paths.get("file.txt"), StandardCharsets.UTF_8); } catch (IOException due east) { // TODO Auto-generated grab block e.printStackTrace(); } System.out.println("Content of Listing:"); Arrangement.out.println(lines); // reading text file into ArrayList in Java half-dozen BufferedReader bufReader = new BufferedReader(new FileReader("file.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line ! = nil) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); Arrangement.out.println("Content of ArrayLiList:"); Organization.out.println(listOfLines); } } Output Content of List : [Python, Ruby, JavaScript] Content of ArrayLiList: [Python, Ruby, JavaScript]
That'due south all about how to read a text file into ArrayList in Java. You can run into information technology'due south very easy in Coffee vii and Coffee eight by using Files.readAllLines() method. Though you should exist mindful of character encoding while reading a text file in Java.
In Java 6 too, the solution using BufferedReader or Scanner is not very difficult to implement, merely the thing you need to think is that you are loading the whole file into memory.
If the file is also big and you don't have enough memory, your program will die by throwing java.lang.OutOfMemoryError: Java Heap Infinite. In short, this solution is merely good for a small files, for the large files y'all should always read by streaming.
Source: https://www.java67.com/2016/07/how-to-read-text-file-into-arraylist-in-java.html
Post a Comment for "Java Reading From a File Line by Line"