java8 read file 读文件
来源:互联网 发布:radius协议认证端口 编辑:程序博客网 时间:2024/06/05 04:17
In Java 8, you can use Files.lines
to read file as Stream
.
c://lines.txt – A simple text file for testing
line1line2line3line4line5
Java 8 Read File + Stream
TestReadFile.java
package com.mkyong.java8;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths;import java.util.stream.Stream;public class TestReadFile {public static void main(String args[]) {String fileName = "c://lines.txt";//read file into stream, try-with-resourcestry (Stream<String> stream = Files.lines(Paths.get(fileName))) {stream.forEach(System.out::println);} catch (IOException e) {e.printStackTrace();}}}
Output
line1line2line3line4line5
Java 8 Read File + Stream + Extra
This example shows you how to use Stream
to filter content, convert the entire content to upper case and return it as aList
.
TestReadFile2.java
package com.mkyong.java8;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 TestReadFile2 {public static void main(String args[]) {String fileName = "c://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 Listlist = stream.filter(line -> !line.startsWith("line3")).map(String::toUpperCase).collect(Collectors.toList());} catch (IOException e) {e.printStackTrace();}list.forEach(System.out::println);}}
Output
LINE1LINE2LINE4LINE5
BufferedReader + Stream
A new method lines()
has been added since 1.8, it lets BufferedReader
returns content as Stream
.
TestReadFile3.java
package com.mkyong.java8;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 TestReadFile3{public static void main(String args[]) {String fileName = "c://lines.txt";List<String> list = new ArrayList<>();try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {//br returns as stream and convert it into a Listlist = br.lines().collect(Collectors.toList());} catch (IOException e) {e.printStackTrace();}list.forEach(System.out::println);}}
Output
line1line2line3line4line5
Classic BufferedReader And Scanner
Enough of Java 8 and Stream
, let revisit the classic BufferedReader
(JDK1.1) and Scanner
(JDK1.5) examples to read a file line by line, it is working still, just developers are moving toward Stream
.
4.1 BufferedReader
+ try-with-resources example.
TestReadFile4.java
package com.mkyong.core;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class TestReadFile4{public static void main(String args[]) {String fileName = "c://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();}}}
4.2 Scanner
+ try-with-resources example.
TestReadFile5.java
package com.mkyong.core;import java.io.File;import java.io.IOException;import java.util.Scanner;public class TestReadFile5 {public static void main(String args[]) {String fileName = "c://lines.txt";try (Scanner scanner = new Scanner(new File(fileName))) {while (scanner.hasNext()){System.out.println(scanner.nextLine());}} catch (IOException e) {e.printStackTrace();}}}
0 0
- java8 read file 读文件
- java8 write file 写文件
- linux kernel read write file 读写文件
- File Read
- read file
- Java8读文件的方法
- read方法读文件
- 读取 wav文件 头信息, Read wav file information
- Linux文件结构FILE,与普通open,read,write对比
- 文件导入sdcard下报错,Read-only file system
- android 虚拟机推送文件提示 read-only file system 解决办法
- android 虚拟机,文件导入sdcard下报错,Read-only file system
- adb向手机写入文件,Read-only file system
- VMware Workstation解压tools文件提示Read-only file system
- Android保存文件时Read-only file system
- Android写文件报read only file system
- db file scattered read(DB 文件分散读取)
- Qt Write and Read XML File 读写XML文件
- Android_Note(二)——主界面功能
- hdu 1247 Hat’s Words (字典树)
- torch入门笔记12:数据的预处理
- 数据库事务隔离级别
- 网站分析方法
- java8 read file 读文件
- 芯片驱动之充电芯片2
- 实现Android下的FPS实时显示工具
- Maven2的配置文件settings.xml
- 测试
- snprintf()函数:最多从源串中拷贝n-1个字符到目标串中,后加一个0
- Android:布局中的view存成图片
- HTML聚美优品
- 扩展,有空慢慢看SpannableString、SpannableStringBuilder与String的关系