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. 【面试题精讲】什么是websocket?如何与前端通信?

    > 有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准`https://blog.zysicyj.top` [首发博客地址](https://blog.zysicyj.t ...

  2. [转帖]一文看懂Linux内核页缓存(Page Cache)

    https://kernel.0voice.com/forum.php?mod=viewthread&tid=629   玩转Linux内核 发布于 2022-8-9 22:19:08 阅读  ...

  3. [转帖]PD Config Learn the PD configuration file

    The PD configuration file supports more options than command-line parameters. You can find the defau ...

  4. [转帖]可直接拿来用的kafka+prometheus+grafana监控告警配置

    kafka配置jmx_exporter 点击:https://github.com/prometheus/jmx_exporter,选择下面的jar包下载: 将下载好的这个agent jar包上传到k ...

  5. [转帖]Linux文件系统的几个性能测试软件小结

    https://developer.aliyun.com/article/297631#:~:text=Linux%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%9A% ...

  6. [转帖]全球CPU市场格局(2022)

    https://www.eet-china.com/mp/a222817.html 本文选自"2022年国产服务器CPU研究框架",重点分析2022年CPU产业链.CPU市场规模. ...

  7. VM PowerCli的简单安装和使用学习

    1. Win10 上面安装 下载 zip包并且进行安装 win10 已经带了powershell 安装比较简单, 只不过安装时会提示 powershell的权限有问题需要打开powershell 执行 ...

  8. Linux 清理 防火墙已有IP地址的方法

    最简单的处理 for i in `firewall-cmd --zone=trusted --list-sources` ;do firewall-cmd --zone=trusted --remov ...

  9. Vue中Vue.set()和this.$forceUpdate()的使用

    1.给对象添加一个key值 成功的 <template> <div> <p>{{userInfo.name}}</p> <p>{{userI ...

  10. ABP-VNext 用户权限管理系统实战03---动态api调用并传递token

    一.使用动态api的目的 ABP可以自动创建C# API 客户端代理来调用远程HTTP服务(REST APIS).通过这种方式,你不需要通过 HttpClient 或者其他低级的HTTP功能调用远程服 ...