码迷,mamicode.com
首页 > 编程语言 > 详细

java7-files读写文件

时间:2015-03-11 18:59:50      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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

带缓冲区的读写器。

java7-files读写文件

标签:

原文地址:http://www.cnblogs.com/duwenlei/p/4330570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!