[IOTest003]FileReader、FileWriter、文本文件复制

FileReader文件字符输入流

int read();

读入到char。Reads a single character.

int read(char[ ] cbuf);

读入到char数组。Reads characters into an array.

int read(char[ ] cbuf, int off, int len);

读入到char数组的指定位置。Reads characters into a portion of an array.

long skip(long n);

跳过n个字符。

void close();

flush(),然后关闭流。

import java.io.*;

public class FileReaderTest01 {
    public static void main(String[] args) {
        try(Reader reader = new FileReader("C:\\Users\\dell\\Documents\\新建文件夹\\test.txt");
        ){
            char[] cbuf = new char[64];
            int readCount;
            //测试FileReader类的skip()方法
            reader.skip(2);
            
            //测试FileReader类的read()方法
            while ((readCount = reader.read(cbuf))!=-1){
                System.out.println(new String(cbuf));//比较一下,如果不指定偏移量和读入的readCount
                System.out.println(new String(cbuf,0,readCount));
            }

        } catch (IOException e) {
        	//TWR语句不手动调用close()
            throw new RuntimeException(e);
        }
    }
}

控制台输出结果:
在这里插入图片描述

FileWriter文件字符输出流

Writer append(char c)

给writer附加某个字符。Appends the specified character to this writer.

Writer append(CharSequence csq)

将指定字符序列追加到写入器。Appends the specified character sequence to this writer.

Writer append(CharSequence csq, int start, int end)

将指定字符序列的子序列追加到写入器。Appends a subsequence of the specified character sequence to this writer.

String 继承于CharSequence。

abstract void close()

flush(),然后关闭流。Closes the stream, flushing it first.

abstract void flush()

Flushes the stream.

static Writer nullWriter()

返回一个空的写入器。Returns a new Writer which discards all characters.

void write(char[ ] cbuf)

写一个char数组。Writes an array of characters.

abstract void write(char[ ] cbuf, int off, int len)

写一个char数组的一部分。Writes a portion of an array of characters.

void write(int c)

写一个字符。Writes a single character.

void write(String str)

写一个字符串。Writes a string.

void write(String str, int off, int len)

写一个字符串的一部分。Writes a portion of a string.

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

public class FileWriterTest01 {
    public static void main(String[] args) {
        try (Writer writer = new FileWriter("C:\\Users\\dell\\Documents\\新建文件夹\\test.txt", true);//如同FileOutputStream,append设true表示续写
        ) {
            //测试append(CharSequence csq)方法向流里追加内容
            writer.append("11111");

            //测试FileWriter类的write()方法
            writer.write("22222");
            writer.write("33333", 0, 2);
            writer.write("44444".toCharArray(), 0, 3);
            
            writer.append("a");
            writer.append("ha");
            //手动刷新流
            writer.flush();
        } catch (IOException e) {
            //自动close(),调用flush()后关闭流
            throw new RuntimeException(e);
        }
    }
}

文本文件的复制

注意仅限于文本文件。

import java.io.*;

public class FileReaderWriterCopyTest {
    public static void main(String[] args) {
        try(
                Reader reader = new FileReader("C:\\Users\\dell\\Documents\\新建文件夹\\test.txt");
                Writer writer = new FileWriter("C:\\Users\\dell\\Documents\\新建文件夹\\test1.txt");
                ) {
            char[] cbuf = new char[64];
            int readCount;

            while ((readCount = reader.read(cbuf))!=-1){
                //向writer追加读出的字符串
                writer.append(new String(cbuf,0,readCount));
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值