Java Read in Text File Into Array
Prior to Java 7, reading a text file into an ArrayList involves a lot of boilerplate coding, as you demand to read the file line by line and insert each line into an ArrayList, merely from Java vii onward, y'all can use the utility method Files.readAllLines() to read all lines of a text file into a List. This method returns a List of Cord that contains all lines of files. Later you can convert this List to ArrayList, LinkedList, or whatever listing you want to. Btw, this the quaternary article in the series of reading a text file in Java.
In the before parts, y'all accept learned how to read a file using Scanner and BufferedReader (1). So, reading the whole file as String (2) and finally reading a text file into an array (3 ). This program is non very unlike from those in terms of fundamentals.
We are still going to employ theread() method for Java half dozen 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 lot know how to read a file line by line, either by using Scanner or by using BufferedReader and then reading a text file into ArrayList is non hard for you lot. All you lot need to do is read each line and store that into ArrayList, as shown in the post-obit example:
BufferedReader bufReader = new BufferedReader(new FileReader("file.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line ! = nix) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.shut();
Just remember to close the BufferedReader one time you are done to prevent resource leak, as you don't have a endeavour-with-resource statement in Java half-dozen.
Reading text file into List in Java - Files.readAllLines Example
In Java seven, you don't need to write code to read and store into ArrayList, only call the Files.readAllLines() method and this will return you a list of String, where each element is the corresponding line from the line. Since List is an ordered collection the gild of lines in a file is preserved in the listing. You tin later catechumen this List to ArrayList or whatever other implementation.
here is sample code to read text file into List in JDK 7:
public static List<String> readFileIntoList(String file) { List<String> lines = Collections.emptyList(); try { lines = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8); } take hold of (IOException e) { // TODO Auto-generated take hold of block east.printStackTrace(); } return lines; }
The readAllLines() method accepts a CharSet, you lot can use a pre-defined character ready due east.m. StandardCharsets.UTF_8 or StandardCharsets.UTF_16. You tin can also meet these complimentary Coffee Courses to learn more than well-nigh new file utility classes introduced in Java vii and 8.
Java Program to read text file into ArrayList
Hither is the complete Java program to demonstrate both methods to read a text file into ArrayList. This program commencement teaches you how to do this in JDK vii or Java viii using theFiles.readAllLines() method and later using BufferedReader and ArrayList in Java vi and lower version.
You can compile and run this program from the control prompt or if you desire to run in Eclipse, simply copy and paste in Eclipse Java projection. The Eclipse IDE volition automatically create a source file for y'all.
So just right-click and Run every bit Java Program. Make sure you have file.txt in your classpath. Since I have given the relative path here, make sure you put that file inside the Eclipse project directory.
Reading text file into ArrayList in Java
import coffee.io.BufferedReader; import coffee.io.FileReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; /* * Java Program read a text file into ArrayList in Java 6 * and Java 8. */ public class ReadFileIntoArrayList { public static void main(String[] args) throws Exception { // reading text file into List in Java vii Listing<String> lines = Collections.emptyList(); try { lines = Files.readAllLines(Paths.go("file.txt"), StandardCharsets.UTF_8); } catch (IOException e) { // TODO Auto-generated take hold of block e.printStackTrace(); } System.out.println("Content of List:"); Organization.out.println(lines); // reading text file into ArrayList in Java 6 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.close(); System.out.println("Content of ArrayLiList:"); Organization.out.println(listOfLines); } } Output Content of List : [Python, Ruby, JavaScript] Content of ArrayLiList: [Python, Ruby, JavaScript]
That's all nearly how to read a text file into ArrayList in Coffee. You can see information technology's very easy in Coffee vii and Java 8 by using Files.readAllLines() method. Though you should be mindful of graphic symbol encoding while reading a text file in Java.
In Coffee half dozen also, the solution using BufferedReader or Scanner is not very difficult to implement, just the thing you need to recall is that you are loading the whole file into memory.
If the file is too big and you don't have enough memory, your program volition die by throwing coffee.lang.OutOfMemoryError: Java Heap Space. In short, this solution is only practiced for a modest files, for the large files you 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 "Java Read in Text File Into Array"
Publicar un comentario