Java IO (3) - Reader

前言

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

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

0. 目录

  1. Reader
  2. BufferedReader
  3. InputStreamReader
  4. FileReader
  5. 总结

1. Reader



Reader是一个抽象类,其介绍如下:

Abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) 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 主要方法

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.

注意,不同于stream的是reader读的是char[]。

其常见的子类包括BufferedReader和InputStreamReader,InputStreamReader的子类FileReader也很常见,下面简单介绍一下。

2. BufferedReader

BufferedReader继承Reader,本身的方法非常简单,其官方解释如下:

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

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

简单翻译一下:

从流里面读取文本,通过缓存的方式提高效率,读取的内容包括字符、数组和行。

缓存的大小可以指定,也可以用默认的大小。大部分情况下,默认大小就够了。

2.1. 构造函数

BufferedReader有两个构造函数,其声明如下:

BufferedReader(Reader in)
Creates a buffering character-input stream that uses a default-sized input buffer. BufferedReader(Reader in, int sz)
Creates a buffering character-input stream that uses an input buffer
of the specified size.

一个是传一个Reader,另外一个增加了缓存的大小。

常见的初始化方法

BufferedReader br = new BufferedReader(new FileReader("d:/123.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

第一个方法是读取一个文件;第二个方法是从标准输入读。

2.2. 主要方法

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, which it does. int read()
Reads a single character. int read(char[] cbuf, int off, int len)
Reads characters into a portion of an array. String readLine()
Reads a line of text. boolean ready()
Tells whether this stream is ready to be read. void reset()
Resets the stream to the most recent mark. long skip(long n)
Skips characters.

提供了三种读数据的方法read、read(char[] cbuf, int off, int len)、readLine(),其中常用的是readLine。

3. InputStreamReader

InputStreamReader的介绍如下:

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:

 BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));

也就是说,InputStreamReader是把字节翻译成字符的。

3.1构造函数

InputStreamReader(InputStream in)
Creates an InputStreamReader that uses the default charset. InputStreamReader(InputStream in, Charset cs)
Creates an InputStreamReader that uses the given charset. InputStreamReader(InputStream in, CharsetDecoder dec)
Creates an InputStreamReader that uses the given charset decoder. InputStreamReader(InputStream in, String charsetName)
Creates an InputStreamReader that uses the named charset.

可以看到,InputStreamReader的构造函数会传入一个字符编码,通常用InputStreamReader来解决乱码问题。

3.2. 主要方法

void	close()
Closes the stream and releases any system resources associated with it. String getEncoding()
Returns the name of the character encoding being used by this stream. int read()
Reads a single character. int read(char[] cbuf, int offset, int length)
Reads characters into a portion of an array. boolean ready()
Tells whether this stream is ready to be read.

3.3. 示例代码

经常用InputStreamReader解决乱码问题,示例代码如下:

	private void test() throws Throwable {
BufferedReader in = null;
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"d:/123.txt"), "UTF-8");
in = new BufferedReader(isr);
while (true) {
String lineMsg = in.readLine();
if (lineMsg == null || lineMsg.equals("")) {
break;
}
}
} catch (Throwable t) {
throw t;
} finally {
try {
if (in != null) {
in.close();
}
} catch (Throwable t) {
throw t;
}
}
}

编码集见本文末尾。

4. FileReader

其介绍如下:

Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

FileReader是方便读取字符文件的。

4.1. 构造函数

FileReader(File file)
Creates a new FileReader, given the File to read from. FileReader(FileDescriptor fd)
Creates a new FileReader, given the FileDescriptor to read from. FileReader(String fileName)
Creates a new FileReader, given the name of the file to read from.

可以看到,FileReader的构造函数主要是读取文件。

5. 总结一下:

1. BufferedReader可以更高效的读取文件

2. InputStreamReader可以处理乱码问题

3. FileReader可以直接读取文件,方便

4. 常见编码

  • 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块

    "US-ASCII"
  • *ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 *

    "ISO-8859-1"
  • *8 位 UCS 转换格式 *

    "UTF-8"
  • *16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 *

    "UTF-16BE"
  • *16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 *

    "UTF-16LE"
  • *16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 *

    "UTF-16"
  • *中文超大字符集 *

    "GBK"

Java IO (3) - Reader的更多相关文章

  1. Java IO: Reader And Writer

    原文链接 作者: Jakob Jenkov  译者: 李璟(jlee381344197@gmail.com) Java IO的Reader和Writer除了基于字符之外,其他方面都与InputStre ...

  2. Java IO 之 InputStream源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  3. Java基础17:Java IO流总结

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

  4. Java io概述

    内容来源:http://ifeve.com/java-io/ Java IO 概述 输入流可以理解为向内存输入,输出流可以理解为从内存输出 Java的IO包主要关注的是从原始数据源的读取以及输出原始数 ...

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

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

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

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

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

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

  8. 【java】io流之字符输入流:java.io.Reader类及子类的子类java.io.FileReader

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

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

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

随机推荐

  1. Image.FrameDimensionsList 属性备注

    Image.FrameDimensionsList 属性 .NET Framework 2.0   获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数. 命名空间:System.D ...

  2. ffmpeg 2.8.1 最新版本 VS2013 可调式动态库

    ffmpeg 2.8.1 最新版本 VS2013 可调式动态库 由于大多数初学者都在想尽各种版本寻求VC编译调试ffmpeg的版本,我也曾经移植过几个版本的ffmpeg到VC上编译.: 链接所需动态库 ...

  3. 查看局域网内某个ip的mac地址

      首先需要ping一下对方的ip,确保本地的arp表中缓存对方的ip和mac的关系 C:\Windows\System32>ping 192.168.1.231 正在 Ping 192.168 ...

  4. java开发之匿名内部类,接口的使用

    下面的例子是Java.JDK7学习笔记上的 打算开发多人联机程序,对每个联机客户端,都会建立Client对象封装相关信息 1.Client.java public class Client { pri ...

  5. float label 提示

    很多时候,我们写input 都会添加 placeholder 属性,用于提示用户这里该输入什么,怎么输入,但是当用户一旦输入了字符串,该提示就会消失,相信会有人,输入内容后可能会忘记这里要输入的是什么 ...

  6. JS创建类以及类的方法(StringBuffeer类)

    创建StringBuffer类以及toString,append()方法 //创建一个StringBuffer类 ,此类有两个方法:一个是append方法一个是toString方法 function ...

  7. find-all-duplicates-in-an-array(典型的数组中的重复数,不错,我做出来了,可是发现别人有更好的做法)

    https://leetcode.com/problems/find-all-duplicates-in-an-array/ 典型的数组中的重复数.这次是通过跳转法,一个个跳转排查的.因为查过的不会重 ...

  8. Qt 多线程学习

    最近的项目上用到了关于多线程的知识,自己也比较感兴趣,所以就拿了那本<C++ GUI Qt4 编程>来学习. 这本书的第14章是关于多线程的知识,使用的Qt版本是Qt4.x.在下用的是最新 ...

  9. nginx - ssl 配置 - globelsign ssl

    前提: 3个文件 - domain.csr.domain.key.xxx.cer 简述: 1. 本地生成 .key文件  [附件] 2. 再利用key文件,生成csr(certificate Secu ...

  10. CF 577B Modulo Sum

    题意:给一个长度为n的正整数序列,问能不能找到一个不连续的子序列的和可以被m整除. 解法:抽屉原理+dp.首先当m<n时一定是有答案的,因为根据抽屉原理,当得到这个序列的n个前缀和%m时,一定会 ...