java7-files读写文件

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("你好我好");
        }
    }
    
}

带缓冲区的读写器。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。