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

    带缓冲区的读写器。

    如果有使用请标明来源:http://www.cnblogs.com/duwenlei/
  • 相关阅读:
    MySQL设置用户
    tensorflow学习笔记12
    tensorflow学习笔记11
    tensorflow学习笔记10
    tensorflow学习笔记9
    博雅机器学习十讲10
    博雅机器学习十讲9
    博雅机器学习十讲8
    博雅机器学习十讲7
    博雅机器学习十讲6
  • 原文地址:https://www.cnblogs.com/duwenlei/p/4330570.html
Copyright © 2020-2023  润新知