在Java8中,可以使用Files.lines读取文件的流。
新建文件 d://lines.txt – 用于测试的简单文本文件
line1 line2 line3 line4 line5 over
1、Java 8 读取文件+流
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class FileTest { public static void main(String[] args) { String fileName = "d://lines.txt"; // read file into stream, try-with-resources try (Stream<String> stream = Files.lines(Paths.get(fileName))) { stream.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } 输出 line1 line2 line3 line4 line5 over
2. Java 8 读取文件+流+额外
这个例子说明了如何使用数据流过滤内容,其全部内容转换为大写并返回一个列表。
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 FileTest { public static void main(String[] args) { String fileName = "d://lines.txt"; List<String> list = new ArrayList<>(); try (Stream<String> stream = Files.lines(Paths.get(fileName))) { // 1. filter line 3 // 2. convert all content to upper case // 3. convert it into a List list = stream.filter(line -> !line.startsWith("line3")).map(String::toUpperCase).collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } list.forEach(System.out::println); } } 输出 LINE1 LINE2 LINE4 LINE5 OVER
3. BufferedReader + Stream
一种新的方法lines() ,已经在JDK1.8已经添加,它让 BufferedReader 返回流的内容。
import java.io.BufferedReader; 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; public class FileTest { public static void main(String[] args) { String fileName = "d://lines.txt"; List<String> list = new ArrayList<>(); try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) { // br returns as stream and convert it into a List list = br.lines().collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } list.forEach(System.out::println); } } 输出 line1 line2 line3 line4 line5 over
4. BufferedReader和Scanner
上面我们已经足够了解Java8和流,现在重温经典BufferedReader(JDK1.1)和Scanner (JDK1.5)的例子来逐行读取文件中的行。
4.1 BufferedReader +资源例子
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileTest { public static void main(String[] args) { String fileName = "d://lines.txt"; try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } 输出 line1 line2 line3 line4 line5 over
4.2 Scanner+资源例子
import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileTest { public static void main(String[] args) { String fileName = "d://lines.txt"; try (Scanner scanner = new Scanner(new File(fileName))) { while (scanner.hasNext()) { System.out.println(scanner.nextLine()); } } catch (IOException e) { e.printStackTrace(); } } } 输出 line1 line2 line3 line4 line5 over
附加
Java8中可以使用一行代码来高效读取文件,可以根据内容转码,比如下面转换UTF-8编码如果内容中有中文,比如下面测试
import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; public class FileTest { public static void main(String[] args) throws IOException { // Java8用流的方式读文件,更加高效 Files.lines(Paths.get("d://java8文件测试.txt"), StandardCharsets.UTF_8).forEach(System.out::println); } } --------输出------------ 第1行 第2行 第3行 第4行 第5行 第6行
如果你在运行中出现了下面错误,那么可能是你的文件编码存在问题
Exception in thread "main" java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1 at java.io.BufferedReader$1.hasNext(Unknown Source) at java.util.Iterator.forEachRemaining(Unknown Source) at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source) at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source) at com.rapido.demo.FileTest.main(FileTest.java:11) Caused by: java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(Unknown Source) at sun.nio.cs.StreamDecoder.implRead(Unknown Source) at sun.nio.cs.StreamDecoder.read(Unknown Source) at java.io.InputStreamReader.read(Unknown Source) at java.io.BufferedReader.fill(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) ... 5 more
解决方法也很简单,另存为utf-8编码即可