优雅读取文件
List<String> lines = Files.readAllLines(Paths.get("/Users/cong/3.txt"), StandardCharsets
.UTF_8);
优雅的写入文件
PrintStream out = new PrintStream(newFileOutputStream("/Users/cong/Desktop/pass/passwd.txt"));
out.println(stringBuilder.toString());
读取大文件
try {
FileChannel fileChannel = FileChannel.open(Paths.get("/Users/cong/Downloads/crackstation-human-only.txt"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
MappedByteBuffer mbb = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, 900);
Charset charset = Charset.forName("utf-8");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(mbb);
Scanner sc = new Scanner(charBuffer).useDelimiter(System.getProperty("line.separator"));
while (sc.hasNext()) {
String next = sc.next();
System.out.println(next);
}
} catch (Exception e) {
e.printStackTrace();
}
网友评论