Java IO (4) - Writer

前言

JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包。Java 流在处理上分为字符流和字节流。字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符、字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组。

Java 内用 Unicode 编码存储字符,字符流处理类负责将外部的其他编码的字符流和 java 内 Unicode 字符流之间的转换。而类 InputStreamReader 和 OutputStreamWriter 处理字符流和字节流的转换。字符流(一次可以处理一个缓冲区)一次操作比字节流(一次一个字节)效率高。

0. 目录

  1. Writer
  2. BufferedWriter
  3. OutputStreamWriter
  4. FileWriter

1. Writer

Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

1.1. 主要方法

Writer	append(char c)
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. abstract void close()
Closes the stream, flushing it first. abstract void flush()
Flushes the stream. void write(char[] cbuf)
Writes an array of characters. abstract void write(char[] cbuf, int off, int len)
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.

同Reader类似,本文介绍一下BufferedWriter和OutputStreamWriter,FileWriter。

2. BufferedWriter

Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.

2.1. 构造函数

BufferedWriter(Writer out)
Creates a buffered character-output stream that uses a default-sized output buffer. BufferedWriter(Writer out, int sz)
Creates a new buffered character-output stream that uses an output buffer
of the given size.

2.2. 主要方法

void	close()
Closes the stream, flushing it first. void flush()
Flushes the stream. void newLine()
Writes a line separator. void write(char[] cbuf, int off, int len)
Writes a portion of an array of characters. void write(int c)
Writes a single character. void write(String s, int off, int len)
Writes a portion of a String.

2.3. 需要注意的地方

2.3.1. flush方法

需要注意里面的flush方法

flush方法的说明是这样的

Flushes the stream.

也就是说会把内容flush进去,冲进去。

那什么时候用这个呢?答案就是,基本上不用。

先看看flush干了什么事情。

  public void flush()
throws IOException
{
synchronized (this.lock)
{
flushBuffer();
this.out.flush();
}
}

就是调用了flushBuffer()方法。

那么close的时候干了什么事情呢?

  public void close()
throws IOException
{
synchronized (this.lock)
{
if (this.out == null) {
return;
}
try
{
flushBuffer();
}
finally
{
this.out.close();
this.out = null;
this.cb = null;
}
}
}

注意,也调用了flushBuffer()。也就是说,只要你close了,就不用flush了。

而且,write也会flush,其代码如下:

  public void write(int paramInt)
throws IOException
{
synchronized (this.lock)
{
ensureOpen();
if (this.nextChar >= this.nChars) {
flushBuffer();
}
this.cb[(this.nextChar++)] = ((char)paramInt);
}
}

那close和write都flush了,flush还有啥用呢?有人这么说:

In other words, relax - just write, write, write and close

Java IO (4) - Writer的更多相关文章

  1. Java IO 四大附加接口、try-with-resource

    Java IO 四大附加接口.try-with-resource @author ixenos 四大附加接口 Closeable.Flushable.Readable.Appendable Close ...

  2. Java:IO流之字符流Reader、Writer详解

    java.io包中:字符流   字符流的两个抽象基类:   Reader         Writer   文件的读取:Reader抽象类(java.io包中) 直接子类的构造方法: FileRead ...

  3. java IO文件读写例子(OutputStream,InputStream,Writer,Reader)

    一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...

  4. java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝

    接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...

  5. 【java】io流之字符输出流:java.io.Writer类及子类的子类java.io.FileWriter

    package 文件操作; import java.io.File; import java.io.FileWriter; import java.io.IOException; import jav ...

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

    声明 我看的是java7的API文档. 如下图所示,java.io.writer 继承了java.lang.Object,实现的接口有Closeable, Flushable, Appendable, ...

  7. 系统学习 Java IO (十三)----字符读写 Reader/Writer 及其常用子类

    目录:系统学习 Java IO---- 目录,概览 Reader Reader 类是 Java IO API 中所有 Reader 子类的基类. Reader 类似于 InputStream ,除了它 ...

  8. Java IO流详解(四)——字符流Reader和Writer

    前面一章介绍了字节流的使用,提到了字节流在处理utf-8编码的中文可能会出现乱码的情况(其他编码的中文同样会出现乱码),所以Java针对这一情况提供了字符流. 但是字符流只能处理字符,不能用来处理 . ...

  9. Java IO: Reader和Writer

    作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) Reader 原文链接 Reader是Java IO中所有Reader的基类.Reader与Input ...

随机推荐

  1. Android权限安全(7)binder,service,zygote安全相关简介

    binder 提供服务的service中的binder thread 检查调用者的uid 不是root,system就异常. service 也检查调用者的uid 不是root,system,只能注册 ...

  2. openVPN使用

    http://www.williamlong.info/archives/3814.html http://openvpn.ustc.edu.cn/ http://www.williamlong.in ...

  3. Codeforces Beta Round #9 (Div. 2 Only)D

    短小精悍的代码 dp[i][j] +=dp[k][j-1]*[i-k-1][j-1] i个结点 J层 #include <iostream> #include<cstdio> ...

  4. linux delete files older than 3 days

    4 down vote accepted This is easy enough (although note that this goes by a modification time more t ...

  5. 一个简单的iBatis入门例子

    一个简单的iBatis入门例子,用ORACLE和Java测试 目录结构: 1.导入iBatis和oracle驱动. 2.创建类Person.java package com.ibeats;import ...

  6. uva12169 Disgruntled Judge

    扩展欧几里得. 枚举a,根据x1,x3和递推式可得. (a+1)*b-k*mod=f[3]-a*a*b. 通过扩展欧几里得求出b. 带入原式进行计算. #include<cstdio> # ...

  7. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)

    题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...

  8. 私有pod简记

    http://www.jianshu.com/p/7a82e977281c http://www.jianshu.com/p/ddc2490bff9f 两个工程 1 代码工程 在github上创建一个 ...

  9. iOS开发实践:一个类微博客户端从启动到与用户交互的过程

    本文基于数据字典和数据流图两种工具讲述一个完整微博客户端的实现.数据字典和数据流图都可以用来表达线程的执行流程,同时定义了需要的类,是进一步设计类的基础. 数据字典实际上是一张表,表的第一个字段是程序 ...

  10. Delphi or函数的用法

    function GetFlag(a: string): Integer;var I: Integer;begin Result := 0; for I := 0 to 3 - 1 do begin ...