java温故而知新(6)深入理解IO Stream
一、什么是IO Stream
二、IO Stream分类
三、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()
四、经典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的更多相关文章
- java.io.stream
1. package com.io.Stream; import java.io.*; public class NyFileInputStream1 { /** * 读取文件的streamIO * ...
- JAVA基础(10)——IO、NIO
转载:http://blog.csdn.net/weitry/article/details/52964948 JAVA基础系列规划: JAVA基础(1)——基本概念 JAVA基础(2)——数据类型 ...
- 少啰嗦!一分钟带你读懂Java的NIO和经典IO的区别
1.引言 很多初涉网络编程的程序员,在研究Java NIO(即异步IO)和经典IO(也就是常说的阻塞式IO)的API时,很快就会发现一个问题:我什么时候应该使用经典IO,什么时候应该使用NIO? 在本 ...
- 从字节码看java类型转换【 深入理解 (T[]) new Object[size] 】
我们都知道,java中对类型的检查是很严格的,所以我们平操作时,也往往很小心. 如题: (T[]) new Object[size],这种写法是一般我们是不会干的!但是有点经验的同学,还是会遇到这样写 ...
- Elasticsearch搜索异常-------org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: parse_exception
异常问题: Caused by: org.elasticsearch.index.query.QueryShardException: Failed to parse query [LOL: Uzi和 ...
- Java网络编程和NIO详解2:JAVA NIO一步步构建IO多路复用的请求模型
Java网络编程与NIO详解2:JAVA NIO一步步构建IO多路复用的请求模型 知识点 nio 下 I/O 阻塞与非阻塞实现 SocketChannel 介绍 I/O 多路复用的原理 事件选择器与 ...
- 沉淀再出发:关于java中的AQS理解
沉淀再出发:关于java中的AQS理解 一.前言 在java中有很多锁结构都继承自AQS(AbstractQueuedSynchronizer)这个抽象类如果我们仔细了解可以发现AQS的作用是非常大的 ...
- 多线程系列之 java多线程的个人理解(二)
前言:上一篇多线程系列之 java多线程的个人理解(一) 讲到了线程.进程.多线程的基本概念,以及多线程在java中的基本实现方式,本篇主要接着上一篇继续讲述多线程在实际项目中的应用以及遇到的诸多问题 ...
- io流中的装饰模式对理解io流的重要性
为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式. 装饰(decorator)你也可以翻译成修饰.比如:一个会精通化学数学的物理学家.在这个"物理学家&q ...
随机推荐
- jquery源码解析:jQuery延迟对象Deferred(工具方法)详解2
请接着上一课继续看. $.Deferred()方法中,有两个对象,一个是deferred对象,一个是promise对象. promise对象有以下几个方法:state,always,then,prom ...
- Java 之封装
预备知识: public:可以被所有其他类所访问 private:只能被自己访问和修改 protected:自身.子类及同一个包中类可以访问 default:同一包中的类可以访问,声明时没有加修饰符, ...
- Android app ADB命令
* 查看设备 adb devices ps这个命令是查看当前连接的设备, 连接到计算机的android设备或者模拟器将会列出显示 若有多台安卓设备,可以通过在adb后面加上 -s <设备id&g ...
- POJ1475 Pushing Boxes(双搜索)
POJ1475 Pushing Boxes 推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...
- P4304 [TJOI2013]攻击装置 最小割
$ \color{#0066ff}{ 题目描述 }$ 给定一个01矩阵,其中你可以在0的位置放置攻击装置. 每一个攻击装置(x,y)都可以按照"日"字攻击其周围的8个位置(x-1, ...
- CocoaPods的PodSpec.json文件用法
最近有时候用最新的CocoaPod的第三方库,有时候发现CocoaPod.org能搜到那个Podfile,但是每次在终端Pod search xxx,每次都搜不到,原来是本地的Podspec没用更新, ...
- Linx 的组管理和权限管理
Linux组基本介绍 在linux中的每个用户必须属于一个组,不能独立于组外.在linux中每个文件 有所有者.所在组.其它组的概念. 1) 所有者 2) 所在组 3) 其它组 4) 改变用户所在的组 ...
- Oracle SQL Developer 查询时间格式
工具->首选项->数据库->NLS->日期格式: DD-MON-RR 修改为: YYYY-MM-DD HH24:MI:SS
- Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.xService }: app is in background uid UidRecord问题原因分析(二)
应用在适配Android 8.0以上系统时,会发现后台启动不了服务,会报出如下异常,并强退: Fatal Exception: java.lang.IllegalStateException Not ...
- JS框架设计之模块加载系统
任何语言一到大规模应用阶段,必然要拆封模块,有利于维护和团队协作,与Java走得最近的dojo率先引进了加载器,使用document.write与同步Ajax请求实现,后台dojo以JSONP的方法来 ...