一点想法:
1、read方法的返回值是int,所以如果将接收到的read返回值打印出来,方式为字符的话需要强转,这一点木有疑惑了。
2、硬要探讨为什么write方法传入的是int,打开被写入的文件变成了字符,我翻了Writer类中写入单字符的write方法,如下:
- public void write(int c) throws IOException {
- synchronized (lock) {
- if (writeBuffer == null){
- writeBuffer = new char[writeBufferSize];
- }
- writeBuffer[0] = (char) c;
- write(writeBuffer, 0, 1);
- }
- }
复制代码 writeBuffer[0] = (char) c;关键代码就是这句,so ez( 简单),传进去的int 被强转了。
|