I/O Streams

Byte Streams

输入输出以字节为单位,所有的使用字节流的类都继承自 InputStreamOutputStream

Byte Streams 属于 low-level I/O,其实是应该避免使用的,效率不高,之所以要提到 Byte Streams 是因为其他的 I/O 流是基于它实现的。

举例:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyBytes {
public static void main(String[] args) throws IOException { FileInputStream in = null;
FileOutputStream out = null; try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c; while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
// 及时关闭流文件
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

机制如下:

Character Streams

Java 使用 Unicode 来存储字符值,字符流会自动转换该格式为本地字符集。

举例:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class CopyCharacters {
public static void main(String[] args) throws IOException { FileReader inputStream = null;
FileWriter outputStream = null; try {
inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("characteroutput.txt"); int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}

CopyCharacters 的实现和 CopyBytes 非常像,只是其中的接口调用不太一样。在 CopyCharacters 中,int c 是一个字符的值至少 16bits,在 CopyBytes 中,int c 是一个字节值至少 8bits

Character Streams 实际使用的是 Byte Streams,只是字符流会在 字符字节 之间做一层自动的转化。

byte-to-character 转化时可以用到 InputStreamReader OutputStreamReader 这两个方法,在没有 prepackaged character 流中可以使用这些方法创建字符流。在 socket 中使用较多。

Line-Oriented I/O

以行为单位进行文件的读取与写入。

举例:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException; public class CopyLines {
public static void main(String[] args) throws IOException { BufferedReader inputStream = null;
PrintWriter outputStream = null; try {
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new PrintWriter(new FileWriter("characteroutput.txt")); String l;
// 读取每一行
while ((l = inputStream.readLine()) != null) {
// 以行方式写入
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}

Buffered Streams

上面提到的几种流都是 unbuffered I/O,他们的每次读或者写都会直接调用底层的的接口进行读写,这样其实效率不高,并且一直触发硬盘的读写或者网络数据的读取或写入。为了提高效率,引入 buffered I/O ,对于写操话数据是优先被缓存起来,待 buffer 满的时候再写入硬盘,对于读操作,在 buffer 是空的时候会调用 input API(Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.)。

对于一个不具有缓存的流可以转化成可缓存的流,如下:

inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));

具有缓存功能的有以下几个类:

  1. BufferedInputStream (byte stream)
  2. BufferedOutputStream (byte stream)
  3. BufferedReader (character streams)
  4. BufferedWriter (character streams)

在特殊某些情况下,我们需要及时将缓冲中的数据进行写入,可以手动调用 flush() 来实现,但其实在 close() 的时候会调用 flush().

void flush()
throws IOException
Flushes this stream by writing any buffered output to the underlying stream.
Throws:
IOException - If an I/O error occurs

参考:

blog

java doc

Java I/O streams的更多相关文章

  1. JAVA Lambda Expressions streams

    http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html https:// ...

  2. Eclipse Collections:让Java Streams更上一层楼

    \ 关键要点 \\ Eclipse Collections是一个高性能的Java集合框架,为原生JDK集合增加了丰富的功能.\\t Streams是JDK的一个非常受欢迎的功能,但它缺少了一些特性,严 ...

  3. java.lang.Exception: No tests found matching Method tes(com.bw.test.Testrefiect) from org.junit.vintage.engine.descriptor.RunnerRequest@3bfdc050 at org.junit.internal.requests.FilterRequest.getRunner

    junit   方法  没有加上注解  @Test java.lang.Exception: No tests found matching Method tes(com.bw.test.Testre ...

  4. Java 8 Concurrency Tutorial--转

    Threads and Executors Welcome to the first part of my Java 8 Concurrency tutorial. This guide teache ...

  5. 和朱晔一起复习Java并发(一):线程池

    和我之前的Spring系列文章一样,我们会以做一些Demo做实验的方式来复习一些知识点. 本文我们先从Java并发中最最常用的线程池开始. 从一个线程池实验开始 首先我们写一个方法来每秒一次定时输出线 ...

  6. No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, org.springframework.boot.logging.LogLevel>]

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  7. 10G R2 参数文件相关

    CLUSTER_DATABASE Property Description Parameter type Boolean Default value false Modifiable No Range ...

  8. 深入解析Oracle 10g中SGA_MAX_SIZE和SGA_TARGET参数的区别和作用

    原文链接:http://m.blog.csdn.net/blog/aaron8219/40037005 SGA_MAX_SIZE是从9i以来就有的作为设置SGA大小的一个参数,而SGA_TARGET则 ...

  9. ORACLE 11G内存管理方式

    SGA包含的组件: 组件名 说明 参数 buffer cache 存放从数据文件中读取的数据拷贝,所有用户之间是可以共享的 db_cache_size db_keep_cache_size db_re ...

随机推荐

  1. MySQL创建临时表

    drop TEMPORARY table if EXISTS temp_table; create TEMPORARY table temp_table( id int not null, usern ...

  2. selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)

    跟你说,你总是靠那个firebug,chrome的F12啥的右击复制xpath绝对总有一天踩着地雷炸的你死活定位不到,这个时候就需要自己学会动手写xpath,人脑总比电脑聪明,开始把xpath语法给我 ...

  3. eas之控制kdtable滚动条

    //滚动条支持三种状态 自动 隐藏 显示 public static final int SCROLL_STATE_AUTO=0://自动根据数据判断是否显示或隐藏 public static fin ...

  4. 【剑指Offer】39、平衡二叉树

      题目描述:   输入一棵二叉树,判断该二叉树是否是平衡二叉树.这里的定义是:如果某二叉树中任意结点的左.右子树的深度相差不超过1,那么它就是一棵平衡二叉树.   解题思路:   首先对于本题我们要 ...

  5. QBXT春季培训酱油记

    Day-1: 下午回家收拾东西,明天去JN,先通知一下在JN的lll dalao明天去见他,然而手办到了,心情大好啊有没有,有没有! 晚上单曲循环<初音未来的消失>,睡觉的时候哭得稀里哗啦 ...

  6. Git 基础教程 之 创建版本库

    一,选择一个合适的地方,创建空目录,下面两种方法都可 ①   手动新建 ②   使用命令: mkdir pythonwork 二,初始化,使目录变成Git可管理的仓库        执行: git i ...

  7. 邓_ SVN·最新使用教程总结

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  8. qt的关闭窗口

    .关闭主窗口并退出程序是 QApplication::exit() .如果是QDialog,就accept() 或 reject()在调用窗口中获取相关参数:void MainWindow::on_p ...

  9. [SDOI2008]沙拉公主的困惑 线性筛 素数+欧拉

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [SDOI2008]沙拉公主的困惑 线性筛 素数+欧拉 题目大意 给定n,m,求在1到n!内与m!互质的 ...

  10. hdu 1713求分数的最小公倍数

    题意中的圈数和天数说反了 #include<stdio.h> __int64 gcd(__int64 a,__int64 b) {/* 比如4/3 3/5 通分20/15 9/15 所以这 ...