Reading an Existing File
Reading a file can be done in a simple way. The question is about where you will store the data once you have it. We will work with lists, iterate through the lists, and then print out the results to System.out
. The next example uses readAllLines()
to open the existing file and reads the contents into the computer's memory, putting them into the fileContent
list. After that, we use an iterator to go through each line and send them to the Terminal:
import java.io.IOException; import java.nio.file.*; import java.util.List; public class Example08 { public static void main(String[] args) { String pathString = System.getProperty("user.home") + "/javaTemp/temp.txt"; Path pathFile = Paths.get(pathString); try { ...