JavaIOStream_第1页
JavaIOStream_第2页
JavaIOStream_第3页
JavaIOStream_第4页
JavaIOStream_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、stream 是在编程语言中对输入输出的总称 (一种比喻的称谓。stream 为流水,输入输出实际上就是指数据的流动,数据由一个地方运动的另一个地方,就像流水一样,程序员将输入输出比作流水,再恰当不过了。)流按照其所载内容分类,大致可分为字节流和字符流两大类:字节流 (byte stream )在计算机中,byte 是相当于机器语言中的单词,他在java 中统一由inputstream和outputstream 作处理。字符流(character stream )而在编码体系中,一般采用char (2 bytes ), 他在java 中统一由reader 和writer 作处理。inputst

2、ream, outputstream, reader 和writer, 作为在java.io.* 包的顶级父类,定义了io process 中最抽象的处理和规范。对于实际的应用,他们并不适用。于是根据各种实际的需要,由他们派生出来形式各样各具特色的子类。下表概述了java io 常用classes 的关系:常用java io classes 关系图byteinputstream/ outputstreamnodebyte streamfileinputstream/ fileoutputstream pipeinputstream/pipeoutputstream processing byt

3、e streamfilterinputstream/filteroutputstreambufferinputstream/bufferoutputstreamdatainputstream/dataoutputstreamprintstream byte 与 char 通过 inputstreamreader 和 outputstreamwriter 来转换charreader/writernodechar streamfilereader/filewriter pipereader/pipewriter processing char stream bufferreader/bufferw

4、riterprintwriter (一)stream 的分类:1 node stream : 基本流,可以从名称中看出他是从哪个地方输入输出的。1.1 用于文件输入输出流: fileinputstream, fileoutputstream1.2 用于内存数组的输入输出流:bytearrayinputstream, bytearrayoutputstream1.3 用于字符串的输入输出流:stringarrayinputstream, stringarrayoutputstream1.4 用于管道的输入输出流:pipedinputstream, pipeoutstream ( 用于线程间的交互

5、).2 processing stream: 处理流,是对node stream 的加强和补充,可以看作是高级流。要构造一个高级流通常要以一个基础流为基础(如通过构造函数的参数传入)2.1 用于提高输入输出效率的缓冲流:bufferedinputstream, bufferedoutputstream2.2 用于数据转化的数据流: datainputstream ( 用于读取java 的primitive data type) , dataoutputstream2.3 8 位转化为16 位的流: inputstreamreader, outputwriter ( 用于沟通byte 和char

6、 )2.4 打印流: pintstream. (二)几个重要的io classesinputstreamabstract int( 可对应char)read () reads the next byte of data from the input streamintread (byte b) reads some number of bytes from the input stream and stores them into the buffer array b.voidclose () closes this input stream and releases any system r

7、esources associated with the stream. (stream 用完之后要注意关闭!) outputstream abstract voidwrite (int b) writes the specified byte to this output stream.voidwrite (byte b) writes b.length bytes from the specified byte array to this output stream.voidclose () closes this output stream and releases any system

8、 resources associated with this stream. voidflush () flushes this output stream and forces any buffered output bytes to be written out. (不必等buffer 满了再写出,强行把所有的东西都写出来) datainputstream能够读出在输入流中读出java 的基本数据类型(primitive data type ),常在对输入流格式十分清楚的情况下使用. booleanreadboolean () bytereadbyte () charreadchar (

9、) doublereaddouble () floatreadfloat () intreadint () dataoutputstream能够直接写出java 的基本数据类型 voidwriteboolean (boolean v) writes a boolean to the underlying output stream as a 1-byte value. voidwritebyte (int v) writes out a byte to the underlying output stream as a 1-byte value.voidwritedouble (double

10、v) converts the double argument to a long using the doubletolongbits method in class double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. voidwritefloat (float v) converts the float argument to an int using the floattointbits method in class

11、 float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. voidwriteint (int v) writes an int to the underlying output stream as four bytes, high byte first. filereader constructor summaryfilereader ( file file) creates a new filereader, given the f

12、ile to read from. filereader ( string filename) creates a new filereader, given the name of the file to read from. filewriterconstructor summaryfilewriter ( file file) constructs a filewriter object given a file object. filewriter ( file file, boolean append) constructs a filewriter object given a f

13、ile object. filewriter ( string filename) constructs a filewriter object given a file name. filewriter ( string filename, boolean append) constructs a filewriter object given a file name with a boolean indicating whether or not to append the data written. printwriter 最好的writer ( 提供了我们熟悉的println() 方法

14、)constructor summaryprintwriter ( file file) creates a new printwriter, without automatic line flushing, with the specified file. printwriter ( outputstream out) creates a new printwriter, without automatic line flushing, from an existing outputstream. printwriter ( writer out) creates a new printwr

15、iter, without automatic line flushing. voidprintln (boolean x) prints a boolean value and then terminates the line. voidprintln (char x) prints a character and then terminates the line. voidprintln (double x) prints a double-precision floating-point number and then terminates the line. voidprintln (

16、float x) prints a floating-point number and then terminates the line. voidprintln (long x) prints a long integer and then terminates the line. voidprintln ( object x) prints an object and then terminates the line. voidprintln ( string x) prints a string and then terminates the line. bufferedreaderin

17、tread () reads a single character. stringreadline () reads a line of text.voidclose () closes the stream and releases any system resources associated with it. bufferedwritervoidwrite (char cbuf, int off, int len) writes a portion of an array of characters. voidwrite ( string s, int off, int len) wri

18、tes a portion of a string.voidclose () closes the stream, flushing it first. voidflush () flushes the stream. inputstreamreaderconstructor summaryinputstreamreader ( inputstream in) creates an inputstreamreader that uses the default charset. outputstreamwriterconstructor summaryoutputstreamwriter (

19、outputstream out) creates an outputstreamwriter that uses the default character encoding. (三)io 编程的一般流程:1. 创建基本流2. 升级基本流到高级流3. 使用在高级流中的方法作读写操作4. 关闭流并释放资源典型例子:-1. 创建 inputstream/reader2. 升级到 buffered3.使用方法 readline() while(str=in.readln()!=null)4. 关闭流,close()-1. 创建 outputstream/writer2. 升级到 printwriter3. 使用方法 println()4. 关闭流,close()-(四)经典的io代码(需要背诵在心) import java.io.* ;public class ioprocesssample public static void main (string args ) string str ; file file = new file ("f:/下载/test.txt" ); bufferedreader in = new bufferedreader (new inputs

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论