package com.du20150311Files; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import org.junit.Test; public class TestFiles { /** * 读文件 * @throws IOException */ // @Test public void test1() throws IOException{ Path path = Paths.get("D://service.csr"); try(BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset());){ String line = ""; while((line = reader.readLine()).length() != 0){ System.out.println(reader.readLine()); } } } /** * 写 * @throws IOException */ @Test public void test2() throws IOException{ Path path = Paths.get("D://service.csr"); try(BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8,StandardOpenOption.APPEND);){ writer.append("你好我好"); } } }
带缓冲区的读写器。