zoukankan      html  css  js  c++  java
  • 文本文件写入

    写入文本文件

    1. 关联读入的文件,使用Reader 和 FileReader

    2. 关联写出的文件,使用Writer和 FileWriter

    3. 创建缓冲 char数组,用于接收读取到的文本信息

    4. 将文本读入到 缓冲数组(buff)中

    5. 输出读取到的文本信息

    6. 写出读取到的文件

    7. 关闭写出文件流

    8. 关闭读取文件流

    package com.machuang.io.charIO;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    
    public class textWrite {
    
        public static void main(String[] args) {
            // 与文件建立联系
            Reader reader = null;
            Writer writer = null;
            
            try {
                reader = new FileReader("F:/win10/test/a.txt");
                writer = new FileWriter("F:/win10/test/aCopy.txt", true);
                
                // 创建 char 字符串缓冲数组
                char[] cbuf = new char[1024];
                
                // 读取和写入
                int len = 0;
                while(-1 != (len = reader.read(cbuf))) {
                    writer.write(cbuf);
                    writer.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(null != writer) {
                        writer.close();
                    }
                    if(null != reader) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
            
    
        } // match main function
    
    }
  • 相关阅读:
    springboot、监听器
    springboot、拦截器
    Thymeleaf模板引擎
    springboot-banner.txt
    springboot,swagger2
    springboot 热部署
    判断是否为微信环境下打开的网页
    后台接收json数据
    ios 面试题
    iOS 适配问题
  • 原文地址:https://www.cnblogs.com/cappuccinom/p/8809818.html
Copyright © 2011-2022 走看看