字符输出流写数据的几个方法

package com.chunzhi.Test05Writer;

import java.io.FileWriter;
import java.io.IOException;

/*
    字符输出流写数据的其它方法:
        void write(char[] cbuf):写入字符数组
        abstract void write(char[] cbuf, int off, int len):写入字符数组的某一部分,off:数组的开始索引,len:写的字符个数
        void writ(String str):写入字符串
        void writ(String str, int off, int len):写入字符数组的某一部分,off:数组的开始索引,len:写的字符个数

 */
public class Test03Writer {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("Day09IOAndProperties\\a.txt");
        char[] chars = {'a', 'b', 'c', 'd', 'e'};
        // void write(char[] cbuf):写入字符数组
        fw.write(chars); // abcde
        // void writ(String str, int off, int len):写入字符数组的某一部分,off:数组的开始索引,len:写的字符个数
        fw.write(chars, 0, 3); // abc

        // void writ(String str):写入字符串
        fw.write("传智播客"); // 传智播客
        //  void writ(String str, int off, int len):写入字符数组的某一部分,off:数组的开始索引,len:写的字符个数
        fw.write("黑马程序员", 2, 3); // 程序员

        // 释放资源
        fw.close();
    }
}

 

posted @ 2020-11-22 20:52  春志  阅读(297)  评论(0)    收藏  举报