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 ...
随机推荐
- [ActionScript 3.0] 框选裁剪
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; ...
- UITableView定制左滑效果
UITableViewRowAction类 object defines a single action to present when the user swipes horizontally in ...
- P1444 [USACO1.3]虫洞wormhole
luogu P1444 [USACO1.3]虫洞wormhole 首先感谢ghj的讲解 题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12 ...
- 箭头函数中的this和普通函数中的this对比
ES6中新增了箭头函数这种语法,箭头函数以其简洁性和方便获取this的特性.下面来总结一下他们之间的区别: 普通函数下的this: 在普通函数中的this总是代表它的直接调用者,在默认情况下,this ...
- HTTP请求处理流程-SpringMvc
1.在SpringMVC的http请求处理过程中,包括了前端控制器(DispatcherServlet).处理映射器(HandlerMapping).处理适配器(HandlerAdapter).处理器 ...
- ORACLE的WITH语句的一个疑惑
使用WITH语句,更新表数据,不行: WITH VN AS ( SELECT T.ID, T.NODE_ID, N.NODE_TYPE, N.NODE_NAME, T.NODE_LEVEL, T.RN ...
- stark - 1 ⇲一些理念
⒈.django项目启动时,自定义执行某个py文件. 在任意的app的apps.py中的Config类中定义ready方法,并调用autodiscover_modules from django.ap ...
- linux如何安装yum
yum全称Yellow dog Updater Modified,yum的主要用途是对rpm包进行管理,包括安装.卸载.升级等.linux安装yum也较为简单,具体如下: 工具/原料 1.电脑: 2. ...
- C++下遍历文件夹
编写程序遍历文件夹及其子文件夹下所有文件,并输出到标准输出流或者文件流. 1. 先考虑在单层目录下,遍历所有文件.以C:\WINDOWS为例: 用到数据结构_finddata_t,文件信息结构体的指针 ...
- oracle12c之四 控制PDB操作 PDBLockdown Profiles
除了IO.内存.CPU之外,还有一些限制,比如:限制在pdb中的操作命令,我们可以创建一个lockdown profile来限制对当前PDB的操作,增强某些操作的安全性. 关于PDB Lockdo ...