一、什么是IO Stream

Stream 是在编程语言中对输入输出的总称 (一种比喻的称谓。Stream 为流水,输入输出实际上就是指数据的流动,数据由一个地方运动的另一个地方,就像流水一样,程序员将输入输出比作流水,再恰当不过了)。
 流按照其所载内容分类,大致可分为字节流和字符流两大类。
1、字节流 (Byte Stream)
在计算机中,byte是相当于机器语言中的单词,他在Java中统一由InputStreamOutputStream作处理。
 
2、字符流(Character Stream)
而在编码体系中,一般采用Char(2 bytes), 他在Java中统一由ReaderWriter作处理。
 
InputStream, OutputStream, Reader和Writer, 作为在java.io.*包的顶级父类,定义了IO Process中最抽象的处理和规范。对于实际的应用,他们并不适用。于是根据各种实际的需要,由他们派生出来形式各样各具特色的子类。
 

二、IO Stream分类

1 Node Stream :基本流,可以从名称中看出他是从哪个地方输入输出的。
1.1 用于文件输入输出流: FileInputStream, FileOutputStream
1.2 用于内存数组的输入输出流:ByteArrayInputStream, ByteArrayOutputStream
1.3 用于字符串的输入输出流:StringArrayInputStream, StringArrayOutputStream
1.4 用于管道的输入输出流:PipedInputStream, PipeOutStream (用于线程间的交互)
….
2 Processing Stream: 处理流,是对Node Stream的加强和补充,可以看作是高级流。 要构造一个高级流通常要以一个基础流为基础(如通过构造函数的参数传入)
2.1 用于提高输入输出效率的缓冲流:BufferedInputStream, BufferedOutputStream
2.2 用于数据转化的数据流: DataInputStream (用于读取Java的Primitive Data Type) , DataOutputStream
2.3 8位转化为16位的流: InputStreamReader, OutputWriter (用于沟通byte 和Char )
2.4 打印流: PintStream
….

三、IO 编程的一般流程

1. 创建基本流
2. 升级基本流到高级流
3. 使用在高级流中的方法作读写操作
4. 关闭流并释放资源
-------------------------------------------------------------------------------
1. Creat node stream;
2. Upgrade node stream to processing stream if necessary
3. Use the methods in the stream object to read or write
4. Close the stream and release the resource
------------------------------------------------------------------------------
1. Create InputStream/Reader
2. Upgrade to Buffered
3. Use readLine()
While((str=in.readln())!=null)
4. close()
------------------------------------------------------------------------------
1. Create OutputStream/Writer
2. Upgrade to PrintWriter
3. Use println()
4. close()

四、经典IO实例

import java.io.*;
/*
1. Creat node stream;
2. Upgrade node stream to processing stream if necessary
3. Use the methods in the stream object to read or write
4. Close the stream and release the resource
--------------------------------------------------------
1. Create InputStream/Reader
2. Upgrade to Buffered
3. Use readLine()
While((str=in.readln())!=null)
4. close()
--------------------------------------------------------
1. Create OutputStream/Writer
2. Upgrade to PrintWriter
3. Use println()
4. close()
*/
public class IOProcessSample{ public static void main(String[] args) {
//Create a file based on the first command-line argument to the program
File file= new File(args[0]);
//Create buffered reader from the standard input
BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Press ctr-d or ctr-z to end");
String str;
try{
//Create a print write to write on a file
//PrintWriter is required to handled the IO exception
PrintWriter out= new PrintWriter(file);
//Read from the standard input and write to the file
while((str=in.readLine())!=null){
out.println(str);
}
//close the stream and release the resource
in.close();
out.close();
}
catch(FileNotFoundException e){
System.err.println("File not found in part 1 : "+file);
}
catch (IOException e){
e.printStackTrace();
}
finally{
System.out.println("-----------Part1 is ended-----------------------");
}
//////////////////////////////////////////////////////////////////////////////
try{
//Create a buffer reader from a file
in=new BufferedReader(new FileReader(file));
//Read the file and print the content on the screen.
while((str=in.readLine())!=null){
System.out.println(str);
}
//close the stream and release the resource
in.close();
}
catch (FileNotFoundException e){
System.err.println("File not found in part 2: "+file);
}
catch (IOException e){
e.printStackTrace();
}
finally{
System.out.println("----------------------The End -------------------------");
}
}
}

java温故而知新(6)深入理解IO Stream的更多相关文章

  1. java.io.stream

    1. package com.io.Stream; import java.io.*; public class NyFileInputStream1 { /** * 读取文件的streamIO * ...

  2. JAVA基础(10)——IO、NIO

    转载:http://blog.csdn.net/weitry/article/details/52964948 JAVA基础系列规划: JAVA基础(1)——基本概念 JAVA基础(2)——数据类型 ...

  3. 少啰嗦!一分钟带你读懂Java的NIO和经典IO的区别

    1.引言 很多初涉网络编程的程序员,在研究Java NIO(即异步IO)和经典IO(也就是常说的阻塞式IO)的API时,很快就会发现一个问题:我什么时候应该使用经典IO,什么时候应该使用NIO? 在本 ...

  4. 从字节码看java类型转换【 深入理解 (T[]) new Object[size] 】

    我们都知道,java中对类型的检查是很严格的,所以我们平操作时,也往往很小心. 如题: (T[]) new Object[size],这种写法是一般我们是不会干的!但是有点经验的同学,还是会遇到这样写 ...

  5. Elasticsearch搜索异常-------org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: parse_exception

    异常问题: Caused by: org.elasticsearch.index.query.QueryShardException: Failed to parse query [LOL: Uzi和 ...

  6. Java网络编程和NIO详解2:JAVA NIO一步步构建IO多路复用的请求模型

    Java网络编程与NIO详解2:JAVA NIO一步步构建IO多路复用的请求模型 知识点 nio 下 I/O 阻塞与非阻塞实现 SocketChannel 介绍 I/O 多路复用的原理 事件选择器与 ...

  7. 沉淀再出发:关于java中的AQS理解

    沉淀再出发:关于java中的AQS理解 一.前言 在java中有很多锁结构都继承自AQS(AbstractQueuedSynchronizer)这个抽象类如果我们仔细了解可以发现AQS的作用是非常大的 ...

  8. 多线程系列之 java多线程的个人理解(二)

    前言:上一篇多线程系列之 java多线程的个人理解(一) 讲到了线程.进程.多线程的基本概念,以及多线程在java中的基本实现方式,本篇主要接着上一篇继续讲述多线程在实际项目中的应用以及遇到的诸多问题 ...

  9. io流中的装饰模式对理解io流的重要性

    为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式. 装饰(decorator)你也可以翻译成修饰.比如:一个会精通化学数学的物理学家.在这个"物理学家&q ...

随机推荐

  1. [ActionScript 3.0] 框选裁剪

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; ...

  2. UITableView定制左滑效果

    UITableViewRowAction类 object defines a single action to present when the user swipes horizontally in ...

  3. P1444 [USACO1.3]虫洞wormhole

    luogu P1444 [USACO1.3]虫洞wormhole 首先感谢ghj的讲解 题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12 ...

  4. 箭头函数中的this和普通函数中的this对比

    ES6中新增了箭头函数这种语法,箭头函数以其简洁性和方便获取this的特性.下面来总结一下他们之间的区别: 普通函数下的this: 在普通函数中的this总是代表它的直接调用者,在默认情况下,this ...

  5. HTTP请求处理流程-SpringMvc

    1.在SpringMVC的http请求处理过程中,包括了前端控制器(DispatcherServlet).处理映射器(HandlerMapping).处理适配器(HandlerAdapter).处理器 ...

  6. ORACLE的WITH语句的一个疑惑

    使用WITH语句,更新表数据,不行: WITH VN AS ( SELECT T.ID, T.NODE_ID, N.NODE_TYPE, N.NODE_NAME, T.NODE_LEVEL, T.RN ...

  7. stark - 1 ⇲一些理念

    ⒈.django项目启动时,自定义执行某个py文件. 在任意的app的apps.py中的Config类中定义ready方法,并调用autodiscover_modules from django.ap ...

  8. linux如何安装yum

    yum全称Yellow dog Updater Modified,yum的主要用途是对rpm包进行管理,包括安装.卸载.升级等.linux安装yum也较为简单,具体如下: 工具/原料 1.电脑: 2. ...

  9. C++下遍历文件夹

    编写程序遍历文件夹及其子文件夹下所有文件,并输出到标准输出流或者文件流. 1. 先考虑在单层目录下,遍历所有文件.以C:\WINDOWS为例: 用到数据结构_finddata_t,文件信息结构体的指针 ...

  10. oracle12c之四 控制PDB操作 PDBLockdown Profiles

    除了IO.内存.CPU之外,还有一些限制,比如:限制在pdb中的操作命令,我们可以创建一个lockdown profile来限制对当前PDB的操作,增强某些操作的安全性.   关于PDB Lockdo ...