JavaSampleApproach will guide you how to read a text file by many approaches: BufferedReader, Java7 and Java8 with Java Read Text File
line1 line2 line3 line4 line5 |
Contents
I. Java Read Text File with Classis Approach – BufferedReader
package com.javasampleapproach.readfilewithclassisapproach; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileWithClassisApproach { public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader(new FileReader("C://readfile/input.txt")); String line; while (null != (line = br.readLine())) { // process each line of File System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != br) br.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
Output:
line1 line2 line3 line4 line5 |
II. Java 7
1. try-with-resources Statement
BufferedReader implements java.lang.AutoCloseable, so we can use it with try-with-resources statement of Java 7 with a shorter and clear sourcecode:
package com.javasampleapproach.readfilewithjava7; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileWithJava7 { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("C://readfile/input.txt"))) { String line; while (null != (line = br.readLine())) { // processing each line of file System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |
2. Files.readAllLines
For a small file, we can use Files.readAllLines to load all lines of file into memory then processing it. But we must be carefully to use it with a large file beacause of memory issues.
package com.javasampleapproach.readfilewithjava7.loadlinestomemory; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class ReadFileWithJava7ByLoadAllLineToMemory { public static void main(String[] args) { try{ List<String> lines = Files.readAllLines(Paths.get("C://readfile/input.txt")); for (String line : lines) { System.out.println(line); } }catch(Exception e){ e.printStackTrace(); } } } |
III. Java 8
1. Read File with Stream
package com.javasampleapproach.readfilewithjava8.sample1; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadFileWithJava8 { public static void main(String[] args) { String filePath = "C://readfile/input.txt"; try (Stream<String> stream = Files.lines(Paths.get(filePath))) { stream.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
2. Read & Process each line of a File by Stream, Filter, and Map functions
package com.javasampleapproach.readfilewithjava8.sample2; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ReadFileWithJava8AndProcessing { public static void main(String[] args) { String filePath = "C://readfile/input.txt"; List<String> list = new ArrayList<>(); try (Stream<String> stream = Files.lines(Paths.get(filePath))) { //1. filter line 1 //2. get end character //3. convert stream into a List list = stream .filter(line -> !line.endsWith("1")) .map(line->line.substring(4,5)) .collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } // process result List list.forEach(System.out::println); } } |
Output:
2 3 4 5 |
IV. Sourcecode
Last updated on June 4, 2017.