Java 字符流操作
上篇文章[Java 字节流操作](http://www.cnblogs.com/yangming1996/p/6549800.html)介绍了java中基本的字节流操作,但是我们常常对于字符操作,如果使用字节流来实现输入输出就显得麻烦,我们可以使用字符流来实现对我们看得见的字符char进行操作,主要内容如下:
- 基本流(Reader/Writer)
- 转换流(InputStreamReader/OutputStreamEWriter)
- 文件字符流(FileReader/FileWriter)
- 字符数组流(charArrayReader/charArrayWriter)
- 缓冲字符流(BufferedReader/BufferedWriter)
- 装饰类(PrintWriter)
一、基本流
字节流的基本流是InputStream/OutputStream,这里的字符流的基本流是Reader/Writer,他们都是抽象类,想要实现更加复杂的操作就必须要子类来扩充。他们内部主要的就是read和write方法,实现单个字符及字符数组的读取的写入。此处就不再列出,我们将会从他们的子类中看看这些方法的实现。
二、转换流
InputStreamReader和OutputStreamWriter这两个类型流,在整个字符流中是十分重要的流,他们实现了和字节流的转换。先看看InputStreamReader:
private final StreamDecoder sd;
public InputStreamReader(InputStream in)
public InputStreamReader(InputStream in, String charsetName)
public InputStreamReader(InputStream in, Charset cs)
public InputStreamReader(InputStream in, CharsetDecoder dec)
public int read() throws IOException {
return sd.read();
}
public int read(char cbuf[], int offset, int length) throws IOException {
return sd.read(cbuf, offset, length);
}
首先定义了一个StreamDecoder 类型的常量(这是一个十分重要的成员),一堆构造方法通过不同的方式指定了外部传入的InputStream类型的参数和解码类型。我们看到,read方法中调用的是上述的sd常量(这是一个StreamDecoder类型的常量)的方法。这个StreamDecoder类实际上完成了将字节转换成char的操作。
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
InputStreamReader inr = new InputStreamReader(new FileInputStream("hello.txt"));
char[] chs = new char[100];
inr.read(chs);
System.out.println(chs);
inr.close();
}
}
上述代码展示了如何从文件中按照指定的解码方式读取出字符,OutputStreamWriter几乎是逆操作:
public void write(int c)
public void write(char cbuf[], int off, int len)
public void write(String str, int off, int len)
可以写int,可以写char数组,还可以写字符串(实际上还是调用了getchars方法获取该字符串的内置char数组,然后调用写数组的方法)。
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream("hello.txt"));
ow.write("walker");
ow.close();
}
}
/*可以明显感知,对字符操作的简单直接*/
三、文件字符流
FileReader和FileWriter两个流,继承的是上述的两个转换流。内部的方法非常简单,只是几个构造方法而已。
public FileReader(String fileName) throws FileNotFoundException {
super(new FileInputStream(fileName));
}
public FileReader(File file) throws FileNotFoundException {
super(new FileInputStream(file));
}
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}
public FileWriter(String fileName, boolean append) throws IOException {
super(new FileOutputStream(fileName, append));
}
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}
从源代码中可以看出来,这两个文件流完全依赖父类。自己基本没有扩展父类,使用的方法都是父类的。你可以通过传文件的路径或者构建File类作为构造参数传入。
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("hello.txt");
char[] chars = new char[1024];
fr.read(chars);
System.out.println(chars);
}
}
一样可以达到读取字符的效果,实际上上述代码可以转换为:
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
InputStreamReader ins = new InputStreamReader(new FileInputStream("hello.txt"));
char[] chars = new char[1024];
ins.read(chars);
System.out.println(chars);
}
}
//因为FIleReader的内部还是通过super调用父类的构造方法
四、字符数组流
和之前介绍的字节数组流类似,都是为了能提高效率防止内存浪费而设计的。看看我们上面的一段代码:
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("hello.txt");
char[] chars = new char[1024];
fr.read(chars);
System.out.println(chars);
}
}
这段程序其实是不完善的,因为我们默认hello文件中的字符容量小于等于1024,那如果文件足够大,我们势必要创建更大的字符数组(这是一种浪费内存)。我们可以使用字符数组流来实现动态扩容,解决内存空间。上述代码可以改写:
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("hello.txt");
int x = 0;
CharArrayWriter chw = new CharArrayWriter();
while ((x = fr.read()) != -1){
chw.write(x);
}
System.out.println(chw.toString());
chw.close();
}
}
这样,即使文件再大,我们也不会浪费很多内存空间。至于StingReader和StringWriter两个流其实是类似的,因为String的本质是char数组, 所以他们必然也是有数组作为最基本的操作。
五、缓冲字符流
字符的缓冲流和字节的缓冲流是类似的。都是装饰流。
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
BufferedReader bins = new BufferedReader(new FileReader("hello.txt"));
BufferedWriter bws = new BufferedWriter(new FileWriter("hello.txt",true));
int x;
while ((x = bins.read()) != -1){
System.out.println((char)x);
bws.write(x);
}
}
}
//缓冲读和缓冲写
六、PrintWriter
这是一个继承与Writer的流,他是一个非常方便的类,可以直接指定文件名作为参数,可以指定编码类型,还支持自动缓冲技术,可以自动转换多种基本类型为字符串形式写入文件中。在我们日常使用写入文件时,可以优先考虑使用该类。
protected Writer out;
private final boolean autoFlush;
//构造方法
public PrintWriter (Writer out) {
this(out, false);
}
public PrintWriter(OutputStream out) {
this(out, false);
}
public PrintWriter(String fileName) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
false);
public PrintWriter(File file) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
false);
}
//写入一个character
public void write(int c) {
try {
synchronized (lock) {
ensureOpen();
out.write(c);
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
//写入一个字符串
public void write(String s) {
write(s, 0, s.length());
}
//重载print方法
public void print(boolean b) {
write(b ? "true" : "false");
}
public void print(char c) {
write(c);
}
public void print(int i) {
write(String.valueOf(i));
}
public void println() {
newLine();
}
public void println(int x) {
synchronized (lock) {
print(x);
println();
}
}
public void println(String x) {
synchronized (lock) {
print(x);
println();
}
}
上述代码中的print和println并非和我们的标准输出流(System.out.println)同义,这里的print表示写入到文件中。
public class Test_InputOrOutput {
public static void main(String[] args) throws IOException{
PrintWriter pw = new PrintWriter("hello.txt");
pw.println('x');
pw.close();
}
}
使用了PrintWriter写入到文件中,非常的简单方便,可以指定文件路径,File,OutputStream作为构造方法的形参。这一切的转换都封装了,自动提供缓冲流。这是一种比较好的输出流,在之后的使用中,如果遇到输出到文件,应该优先考虑PrintWriter。
本篇文章结束了,这两篇文章是我阅读书籍和博客,加上jdk源代码总结得来,如有错误,望大家指出!
Java 字符流操作的更多相关文章
- java字符流操作flush()方法及其注意事项
java字符流操作flush()方法及其注意事项 flush()方法介绍 查阅文档可以发现,IO流中每一个类都实现了Closeable接口,它们进行资源操作之后都需要执行close()方法将流关闭 ...
- Java 字符流实现文件读写操作(FileReader-FileWriter)
Java 字符流实现文件读写操作(FileReader-FileWriter) 备注:字符流效率高,但是没有字节流底层 字节流地址:http://pengyan5945.iteye.com/blog/ ...
- Java字符流和字节流对文件操作
记得当初自己刚开始学习Java的时候,对Java的IO流这一块特别不明白,所以写了这篇随笔希望能对刚开始学习Java的人有所帮助,也方便以后自己查询.Java的IO流分为字符流(Reader,Writ ...
- java的IO操作:字节流与字符流操作
流的概念 程序中的输入输出都是以流形式,流中保存的实际上都是字节文件. 字节流与字符流 字节流的操作: 1)输入:inputStream, 2)输出:outPutStream; 字符流的操作: 1)输 ...
- Java 字符流文件读写
上篇文章,我们介绍了 Java 的文件字节流框架中的相关内容,而我们本篇文章将着重于文件字符流的相关内容. 首先需要明确一点的是,字节流处理文件的时候是基于字节的,而字符流处理文件则是基于一个个字符为 ...
- Java IO流操作汇总: inputStream 和 outputStream【转】
我们在进行Android java 开发的时候,经常会遇到各种IO流操作.IO流操作一般分为两类:字符流和字节流.以“Reader”结尾都是字符流,操作的都是字符型的数据:以“Stream”结尾的都是 ...
- Java 文件流操作.
一.概念 在Java中,文件的输入和输出是通过流(Stream)来实现的.一个流,必有源端和目的端,它们可以是计算机内存的某些区域,也可以是磁盘文件,甚至可以是 Internet 上的某个 URL.对 ...
- java字符流与字节流的区别是什么
java中字符流与字节流的区别: 1.字节流操作的基本单元为字节:字符流操作的基本单元为Unicode码元. 2.字节流默认不使用缓冲区:字符流使用缓冲区. 3.字节流通常用于处理二进制数据,实际上它 ...
- 全面吃透JAVA Stream流操作,让代码更加的优雅
全面吃透JAVA Stream流操作,让代码更加的优雅 在JAVA中,涉及到对数组.Collection等集合类中的元素进行操作的时候,通常会通过循环的方式进行逐个处理,或者使用Stream的方式进行 ...
随机推荐
- I帧/P帧/B帧---术语解释
视频压缩中,每帧代表一幅静止的图像.而在实际压缩时,会采取各种算法减少数据的容量,其中IPB就是最常见的. 简单地说,I帧是关键帧,属于帧内压缩.就是和AVI的压缩是一样的. P是向前搜索的意思.B ...
- 《深度探索C++对象模型》笔记——Data语意学
Data Member的绑定 inline member functin躯体之内的一个data member绑定操作会在整个class声明完成之后才发生. argument list中的名称还是会在它 ...
- .Net多线程编程—误用点分析
1 共享变量问题 错误写法: 所有的任务可能会共享同一个变量,所以输出结果可能会一样. public static void Error() { ;i<;i++) { Task.Run(() = ...
- mesos 资源分配
Mesos 资源分配 众所周知, Mesos在运行时使用wDRF( Dominant Resource Fairness)算法进行一级资源分配, 通过应用程序(Framework)运行时使用资源进行二 ...
- 新手Axis2 发布Web Service之路
由于公司的需求,需要写几个银行接口写模拟器(Mock Server),此次接口需要发布成一个WEB Service. 一开始,我以为只要负责写接口的业务层就行了,具体的框架或是环境搭建可以不用管.在与 ...
- Spark:一个独立应用
[TOC] Spark:一个独立应用 关于构建 Java和Scala 在Java和Scala中,只需要给你的应用添加一个对于spark-core的Maven依赖. Python 在Python中,可以 ...
- ubuntu 笔记一
注:ubuntu14.04 64位 1.刚安装的ubuntu无法在终端使用su 原因:root没有默认密码,需要手动设定. 解决方法:以具有sudo权限的用户登录 给root用户设置密码:打开一个te ...
- C++ Primer 笔记 第三章
C++ Primer 第三章 标准库类型 3.1using声明 例: using namespace atd; using std::cin; 3.2string类型 初始化方式 string s1 ...
- 《JAVASCRIPT高级程序设计》客户端检测
web开发的理想状态之一是浏览器支持一组最常用的功能,但是在现实情况下,浏览器间的差异非常大,因此,为了兼容大部分的了浏览器,开发人员首先需要设计最通用的方案,然后再使用客户端检测的技术增强该方案.客 ...
- asp.net权限认证篇外:集成域账号登录
在之前的我们已经讲过asp.net权限认证:Windows认证,现在我们来讲讲域账号登录, 这不是同一件事哦,windows认证更多的是对资源访问的一种权限管控,而域账号登录更多的是针对用户登录的认证 ...