How to Read File Into Array Java
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 utilize the utility method Files.readAllLines() to read all lines of a text file into a List. This method returns a List of String that contains all lines of files. Later y'all can catechumen this Listing to ArrayList, LinkedList, or whatsoever list you desire to. Btw, this the fourth article in the series of reading a text file in Java.
In the before parts, you take learned how to read a file using Scanner and BufferedReader (1). Then, reading the whole file equally Cord (two) and finally reading a text file into an array (three ). This program is not very different from those in terms of fundamentals.
We are still going to apply 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 Case
If y'all know how to read a file line by line, either past using Scanner or by using BufferedReader then reading a text file into ArrayList is not hard for yous. All yous need to do is read each line and store that into ArrayList, equally shown in the following instance:
BufferedReader bufReader = new BufferedReader(new FileReader("file.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line ! = null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.shut();
Just remember to close the BufferedReader in one case y'all are washed to forestall resources leak, as you don't have a try-with-resources statement in Coffee 6.
Reading text file into List in Java - Files.readAllLines Example
In Java 7, you lot don't demand to write code to read and shop into ArrayList, only call the Files.readAllLines() method and this will return you a listing of String, where each element is the corresponding line from the line. Since List is an ordered collection the order of lines in a file is preserved in the list. You can later catechumen this List to ArrayList or any other implementation.
here is sample code to read text file into Listing in JDK 7:
public static Listing<Cord> readFileIntoList(String file) { Listing<Cord> lines = Collections.emptyList(); try { lines = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8); } take hold of (IOException due east) { // TODO Automobile-generated catch block e.printStackTrace(); } return lines; }
The readAllLines() method accepts a CharSet, yous tin apply a pre-divers character set e.thousand. StandardCharsets.UTF_8 or StandardCharsets.UTF_16. Yous can also encounter these costless Java Courses to learn more about new file utility classes introduced in Java 7 and 8.
Coffee Programme to read text file into ArrayList
Here is the complete Java program to demonstrate both methods to read a text file into ArrayList. This programme first teaches you how to do this in JDK seven or Java 8 using theFiles.readAllLines() method and afterwards using BufferedReader and ArrayList in Coffee 6 and lower version.
You lot can compile and run this programme from the command prompt or if you want to run in Eclipse, simply copy and paste in Eclipse Coffee projection. The Eclipse IDE will automatically create a source file for you.
And so just right-click and Run as Coffee Programme. Brand sure you have file.txt in your classpath. Since I take given the relative path here, make sure you put that file inside the Eclipse project directory.
Reading text file into ArrayList in Coffee
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import coffee.util.ArrayList; import coffee.util.Collections; import coffee.util.List; /* * Java Plan read a text file into ArrayList in Java half dozen * and Java eight. */ public class ReadFileIntoArrayList { public static void chief(Cord[] args) throws Exception { // reading text file into List in Java 7 List<String> lines = Collections.emptyList(); try { lines = Files.readAllLines(Paths.go("file.txt"), StandardCharsets.UTF_8); } catch (IOException eastward) { // TODO Auto-generated catch block e.printStackTrace(); } Organization.out.println("Content of Listing:"); System.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<>(); Cord line = bufReader.readLine(); while (line ! = null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.shut(); System.out.println("Content of ArrayLiList:"); System.out.println(listOfLines); } } Output Content of List : [Python, Cerise, JavaScript] Content of ArrayLiList: [Python, Ruby, JavaScript]
That's all about how to read a text file into ArrayList in Java. You lot can see it's very easy in Java 7 and Java 8 by using Files.readAllLines() method. Though you should be mindful of character encoding while reading a text file in Java.
In Java vi besides, the solution using BufferedReader or Scanner is not very hard to implement, but the thing y'all need to remember is that you are loading the whole file into retention.
If the file is besides big and y'all don't have enough memory, your program will die by throwing coffee.lang.OutOfMemoryError: Coffee Heap Infinite. In short, this solution is only practiced 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
0 Response to "How to Read File Into Array Java"
Post a Comment