Java Writer

Writer是一个用于写字符流的抽象类。其子类必须实现write(char[], int, int), flush(), 和 close()方法。

类定义

public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable

属性

Modifier and Type    Field    Description

protected Object    lock    The object used to synchronize operations on this stream.

构造函数

protected    Writer()

Creates a new character-stream writer whose critical sections will synchronize on the writer itself.

常用方法

Writer        append(char c)                                    往writer追加指定字符

Writer        append(CharSequence csq)                        追加指定字符序列

Writer        append(CharSequence csq, int start, int end)    追加指定字符序列的子序列

abstract     void    close()                                    先冲刷后关闭流

abstract     void    flush()                                    冲刷流

void        write(char[] cbuf)                                写字符数组

abstract     void    write(char[] cbuf, int off, int len)    写字符数组的部分

void        write(int c)                                    写单个字符

void        write(String str)                                写字符串

void        write(String str, int off, int len)                写部分字符串

例子

package com.dylan.io;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer; /**
* @author xusucheng
* @create 2018-01-07
**/
public class WriterExample {
public static void main(String[] args) throws IOException {
Writer w = new FileWriter("D:\\output.txt");
String content = "I love my country.";
w.write(content);
w.close();
System.out.println("Done.");
}
}

Java Reader

Reader 是一个用于读取字符流的抽象类。其子类必须实现的方法只有:read(char[], int, int) 和 close()。



实现类包括: BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader

类定义

public abstract class Reader
extends Object
implements Readable, Closeable

属性

protected Object lock

The object used to synchronize operations on this stream.

构造函数

protected Reader()

Creates a new character-stream reader whose critical sections will synchronize on the reader itself.
protected Reader(Object lock)

Creates a new character-stream reader whose critical sections will synchronize on the given object.

常用方法

abstract void close()

Closes the stream and releases any system resources associated with it.
void mark(int readAheadLimit)

Marks the present position in the stream.
boolean markSupported()

Tells whether this stream supports the mark() operation.
int read()

Reads a single character.
int read(char[] cbuf)

Reads characters into an array.
abstract int read(char[] cbuf, int off, int len)

Reads characters into a portion of an array.
int read(CharBuffer target)

Attempts to read characters into the specified character buffer.
boolean ready()

Tells whether this stream is ready to be read.
void reset()

Resets the stream.
long skip(long n)

Skips characters.

例子

package com.dylan.io;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader; /**
* @author xusucheng
* @create 2018-01-07
**/
public class ReaderExample {
public static void main(String[] args) throws IOException{
Reader r = new FileReader("D:\\output.txt");
int data = 0;
while ((data=r.read())!=-1){
System.out.print((char)data);
} r.close();
}
}

下一章

Java I/O 教程(八) Writer和Reader的更多相关文章

  1. JAVA IO包的整理---------Writer和Reader

    一 Writer public abstract class Writer extends Object implements Appendable, Closeable, Flushable 这个类 ...

  2. 转:Java NIO系列教程(八) DatagramChannel

    Java NIO中的DatagramChannel是一个能收发UDP包的通道.因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入.它发送和接收的是数据包. 打开 DatagramChann ...

  3. Java NIO系列教程(八)JDK AIO编程

    目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...

  4. 20145236 《Java程序设计》第八周学习总结

    20145236 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章 NIO与NIO2 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你 ...

  5. Java NIO系列教程(十一) Java NIO 与 IO

    Java NIO系列教程(十一) Java NIO与IO 当学习了 Java NIO 和 IO 的 API 后,一个问题马上涌入脑海: 我应该何时使用 IO,何时使用 NIO 呢?在本文中,我会尽量清 ...

  6. 20155313 2016-2017-2 《Java程序设计》第八周学习总结

    20155313 2016-2017-2 <Java程序设计>第八周学习总结 教材内容学习 十四章 NIO与NIO2 1.认识NIO NIO使用频道(Channel)来衔接数据节点,在处理 ...

  7. Java NIO系列教程(三-十二) Buffer

    原文链接     作者:Jakob Jenkov     译者:airu     校对:丁一 Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到 ...

  8. Java IO(十八) BufferedReader 和 BufferedWriter

    Java IO(十八) BufferedReader 和 BufferedWriter 一.介绍 BufferedReader 和 BufferedWriter 是字符缓冲流,分别继承自 Reader ...

  9. 简单的Writer和Reader

    Writer用于写出去到文件中,Reader用于将外面的文件读进来控制台 Writer和Reader有许多子类,但是子类大多都是直接运用父类Writer和Reader的方法,而且Writer和Read ...

  10. JavaIO(04)字符流--Writer and Reader

    字符流:   常识:在java中一个字符等于两个字节:   操作字符流的两个类:Writer,Reader   API文档介绍(Writer): public abstract class Write ...

随机推荐

  1. Python学习之十五_不同类型数据库表内容比较

    Python学习只十五_不同类型数据库表内容比较 前言 最近学习力总结了很多Python相关的内容 本次想继续学习一下不同数据库之间的数据比较. 这样理论上可以极大的缩减不同数据库测试成本. 感谢Py ...

  2. [转帖]grafana自定义告警模版

     发表于 2022-03-16  更新于 2023-03-03 因 grafana 告警信息太多无用数据,容易干扰查看例如使用 企业微信告警消息如下太多无用Labels 例如 endpoint,job ...

  3. [转帖]Oracle数据库下PreparedStatementCache内存问题解决方案

    https://github.com/alibaba/druid/wiki/Oracle%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%8BPreparedStatementCac ...

  4. [转帖]tidb集群部署

    http://blog.itpub.net/29785807/viewspace-2789852/ 一.安装规划 1 2 3 4 5 6 使用15台服务器 5台tidb服务器:每台3个tidb实例+1 ...

  5. [转帖]jmeter SSL证书相关配置

    在实际工作中,我们大多数接口都是用的HTTPS来保证安全,使用jmeter测试HTTPS请求是如何配置证书呢? 1.最简单的方法,在选项里选择SSL管理器,然后选择相应的证书即可 在弹出的选择框选择证 ...

  6. [转帖]Jmeter学习笔记(二十三)——生成HTML性能报告

    https://www.cnblogs.com/pachongshangdexuebi/p/11759316.html 有时候我们写性能报告的时候需要一些性能分布图,JMeter是可以生成HTML性能 ...

  7. [转帖]金仓数据库KingbaseES误删除系统超级用户(superuser)权限的恢复方式

    https://blog.csdn.net/arthemis_14/article/details/129879269 在使用KingbaseES数据库的时候,系统默认存在一个跟系统初始化用户同名的S ...

  8. 【转帖】nginx变量使用方法详解-5

    https://www.diewufeiyang.com/post/579.html 前面在 (二) 中我们已经了解到变量值容器的生命期是与请求绑定的,但是我当时有意避开了"请求" ...

  9. Redis7.0 编译安装以及简单创建Cluster测试服务器的方法

    背景 北京时间2022.4.27 晚上九点半左右, Redis 7.0.0 已经GA. 为了进行简单的学习, 这边进行了简单验证工作. 本次主要分为编译, 测试集群搭建,以及springboot进行简 ...

  10. Istio安装和部署

    Istio的版本对k8s的版本是有要求的,不兼容的版本会引发一些隐蔽的错误,安装前先参考下图 版本 目前支持 发行日期 停止维护 支持的 Kubernetes 版本 未测试,可能支持的 Kuberne ...