java io源码解读_java.io.writer API 以及 源码解读

声明 我看的是java7的API文档。

如下图所示,java.io.writer 继承了java.lang.Object,实现的接口有Closeable, Flushable, Appendable, AutoCloseable。

所有直接继承它的子类有BufferedWriter CharArrayWriter FilterWriter OutputStreamWriter PipedWriter PrintWriter StringWriter。

7e26e6186fe282b8f41dca0891e34c59.png

Writer是用来操作字符流的抽象类。所有继承它的子类必须要重写的方法有write(char[], int, int), flush(), and close().

下面是java.io.Writer的源码。

packagejava.io;public abstract class Writer implementsAppendable,Closebale,Flushable{private char[] writeBuffer;private static final int WRITE_BUFFER_SIZE = 1024;

projected Object lock;protectedWriter(){this.lock = this;

}protectedWriter(Object lock){if(lock == null){throw newNullPointerException();

}this.lock =lock;

}public void write(int c) throwIOException{

syschronized (lock){if (writeBuffer == null){

writeBuffer= new char[WRITE_BUFFER_SIZE];

}

writeBuffer[0] = (char) c;

write(writeBuffer,0,1);

}

}public void write(char cbuf[]) throwIOException{

write(cbuf,0, cbuf.length);

}abstract public void write(char buf[], int off, int len) throwIOException;public void write(String str) throwIOException{

write(str,0, str.length());

}public void write(String str, int off, int len) throwIOException{

syschronized(lock){charcbuf[];if(len <=WRITE_BUFFER_SIZE){if(writeBuffer == null){

writeBuffer= new char[WRITE_BUFFER_SIZE];

}

cbuf=writeBuffer;

}else{

cbuf= new char[len];

}

str.getChars(off, (off+ len), cbuf, 0);

write(cbuf,0,len);

}

}public Writer append(CharSequence csq) throwsIOException{if(csq == null)

write("null");elsewrite(csq.toString());return this;

}public Writer append(CharSequence csq, int start, int end) throwIOException{

CharSequence cs= (csq == null ? "null": csq);

write(cs.subSequence(start,end).toString());return this;

}public Writer append(char c) throwIOException{

write(c);return this;

}abstract public void flush() throwIOException;abstract public void close() throwIOException;

}

可以看到在Writer类中子类必须重写的类有三个,

1、abstract public void write(char buf[], int off, int len) throw IOException;

2、abstract public void flush() throw IOException;

3、abstract public void close() throw IOException;

其中,下面三个方法是实现Appendable接口必须实现的方法

1、public Writer append(CharSequence csq) throws IOException

2、public Writer append(CharSequence csq, int start, int end) throw IOException

3、public Writer append(char c) throw IOException

实现 Flushable接口必须实现的方法是

abstract public void flush() throw IOException;

实现Closeable接口必须实现的方法是

abstract public void close() throw IOException;

以下是一个简单的 Java 记账本程序的源代码,包括使用 IO 流进行文件读写操作: ```java import java.io.*; import java.util.Scanner; public class AccountBook { private static final String FILENAME = "accountbook.txt"; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:\n1. 查看记录\n2. 添加记录\n3. 退出"); int choice = scanner.nextInt(); if (choice == 1) { try (BufferedReader reader = new BufferedReader(new FileReader(FILENAME))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("读取文件失败!"); } } else if (choice == 2) { System.out.print("请输入记录:"); String record = scanner.next(); try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILENAME, true))) { writer.write(record + "\n"); System.out.println("记录添加成功!"); } catch (IOException e) { System.out.println("写入文件失败!"); } } else if (choice == 3) { break; } else { System.out.println("无效的选择!"); } } } } ``` 该程序使用了一个常量 `FILENAME` 来表示保存记录的文件名,包含了以下三个主要操作: 1. 查看记录:使用 `BufferedReader` 从文件中读取每一行记录并输出到控制台。 2. 添加记录:使用 `BufferedWriter` 向文件中写入一条记录。 3. 退出:退出程序循环。 在添加记录时,使用了 `FileWriter` 的构造函数的第二个参数 `true`,表示打开文件时追加而不是覆盖原有内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值