java nio 读取文本文件 https://my.oschina.net/changgx/blog/805809
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.changgx.nio;/** * Created by Administrator on 2016/12/14. */ import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Iterator; import java.util.List; import static java.nio.file.Files.readAllBytes; import static java.nio.file.Files.readAllLines; import static java.nio.file.Paths.get; /** * Administrator 2016/12/14 */ public class NioReadFile { public static void main(String[] args) throws IOException { // Path path = Paths.get("E://学习资料//account.txt"); // readAllLines() 默认使用的字符编码是utf-8 List list = (readAllLines(get("E://学习资料//account.txt"), Charset.forName("gbk"))); String str = new String(readAllBytes(get("E://学习资料//account.txt")),"gbk"); System.out.println(list); System.out.println(str); } } |