java输入输出课程

上传人:第*** 文档编号:48873099 上传时间:2018-07-21 格式:PPT 页数:30 大小:177KB
返回 下载 相关 举报
java输入输出课程_第1页
第1页 / 共30页
java输入输出课程_第2页
第2页 / 共30页
java输入输出课程_第3页
第3页 / 共30页
java输入输出课程_第4页
第4页 / 共30页
java输入输出课程_第5页
第5页 / 共30页
点击查看更多>>
资源描述

《java输入输出课程》由会员分享,可在线阅读,更多相关《java输入输出课程(30页珍藏版)》请在金锄头文库上搜索。

1、第七章 输入/输出文件流式I/O基础随机存取文件对象输入/输出流1File类 z一个File类的对象,表示了磁盘上的文件或 目录。 zFile类提供了与平台无关的方法来对磁盘上 的文件或目录进行操作。2文件 Java.io.File 文件类提供获取文件基本信息,以及 其它与文件相关的操作。 创建新的文件对象:File myFile; myFile=new File(“mymotd”);myFile = new File(“”,”mymotd”); 3流Stream的概念流(Stream)是字节的源或目的,从源到目的的字节的有序序 列,先进先出。 两种基本流: Input stream(输入流)

2、,Output stream(输出流 )4流操作的过程输入流(Input Stream)可 从中读出一系列字节 的对象称为输入流。 Reading: open a stream while more informationread information close the stream 输出流(Output Stream)能 向其中写入一系列字节的 对象称为输出流。 Writing :open a stream while more informationwrite information close the stream5两种结构的流 Node Stream(节点流) :从特定源地方读写的

3、 流类,如磁盘文件或内存某区域进行读或写入。 Filter Steam(过滤流):使用已经存在的输入流 或输出流连接创建。使用节点流作为输入或输出 。6两种流类的体系(字节流与字符流) Java.io包中包含了流式I/O所需要的所有类。流式I/O类根据操作的数据类型(16位字符或字 节)分成两个层次体系。7字节流输入流类层次InputStreamStringBufferInputStreamFileInputStreamByteArrayInputStreamFilterInputStreamObjectInputStreamPipedInputStreamSequenceInputStrea

4、mLineNumberInputStreamDataInputStreamBufferedInputStreamPushbackInputStreamjava.io包中 InputStream的类层次8InputStream字节流的方法三个基本read()方法 int read() :读取一个字节数据,并返回读到的数据,读取一个字节数据,并返回读到的数据,如果返回如果返回-1-1,表示读到了输入流的末尾。,表示读到了输入流的末尾。 int read(byte ) :将数据读入一个字节数组,同时返将数据读入一个字节数组,同时返回实际读取的字节数。如果返回回实际读取的字节数。如果返回-1-1,表示

5、读到了输入流的末尾。,表示读到了输入流的末尾。 int read( byte, int offset, int length ):将数据读入一个字节将数据读入一个字节 数组,同时返回实际读取的字节数。如果返回数组,同时返回实际读取的字节数。如果返回-1-1,表示读到了输入,表示读到了输入 流的末尾。流的末尾。offoff指定在数组指定在数组b b中存放数据的起始偏移位置;中存放数据的起始偏移位置;lenlen指定读指定读 取的最大字节数。取的最大字节数。 其它方法 void close( ) /关闭流。自顶向下关闭关闭流。自顶向下关闭Filter streamFilter stream int

6、 available() /返回在不发生阻塞的情况下,未读的字节数返回在不发生阻塞的情况下,未读的字节数 long skip(long n) / 跳过跳过n n个字节个字节 void mark(int) /标记当前流,并建立标记当前流,并建立intint大小缓冲区大小缓冲区 void reset( ) / 返回标签出返回标签出 9字节流输出流类层次OutputStreamFileOutputStreamByteArrayOutputStreamFilterOutputStreamObjectOutputStreamPipedOutputStreamPrintStreamPrintStreamD

7、ataOutputStreamBufferedOutputStreamjava.io包中 OutputStream的类层次10OutputStream字节流方法三个基本的write( )方法 void write( int ) / 写一个字节 void write(byte ) / 写一个字节数组 void write(byte , int offset, int length ) 其它方法void flush( ) :刷新输出流,强制缓冲区中的输出字节被写出。刷新输出流,强制缓冲区中的输出字节被写出。 void close( ) :关闭输出流,释放和这个流相关的系统资源。关闭输出流,释放和这

8、个流相关的系统资源。11基本的字节流类FileInputStream和FileOutputStream节点流,用于从文件中读取或往文件中写入字节流。如果在构造 FileOutputStream时,文件已经存在,则覆盖这个文件。 BufferedInputStream和BufferedOutputStream过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提 高了读写的效率。 DataInputStream和DataOutputStream过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的 基本数据类型的功能。 PipedInputStream和PipedOutputStr

9、eam管道流,用于线程间的通信。一个线程的PipedInputStream对象 从另一个线程的PipedOutputStream对象读取输入。要使管道流有 用,必须同时构造管道输入流和管道输出流。12FileInputStream,FileOutputStream文件流文件流类包括:FileInputStream,FileOutputStream创建文件流:常用文件名或File类的对象创建文件流。例:CopyBytes.java,利用FileInputStream,FileOutputStream。13是过滤流。 数据从原始流成块读入或将数据积累到 一个大数据块后再成批输出。基本方法:int

10、read() int read( byte, int offset, int length )int write(int c) void write(byte , int offset, int length )BufferedReader增加readLine( ) 方法。BufferedInputStream/BufferedOutputStream14 DataInputStream和DataOutputStream(Filter stream) 读写基本数据类型: DataInputStream方法byte readByte( )boolean readBoolean( )long re

11、adLong( )char readChar( )double readDouble( )float readFloat( )short readshort( )int readInt( ) DataOutputStream 方法void writeByte(byte)void writeBoolean(boolean)void writeLong( long )void writeChar(char)void writeDouble(double)void writeFloat( float)void writeshort(short)void writeInt ( int) void wr

12、iteBytes(String)void writeChars(String )DataInputStream/DataOutputStream15示例/example of using inputData public class DataIOTest public static void main(String args) throws IOException / write the data outDataOutputStream out = new DataOutputStream(newFileOutputStream(“invoice1.txt“);double prices =

13、19.99, 9.99, 15.99, 3.99, 4.99 ;int units = 12, 8, 13, 29, 50 ;String descs = “Java T-shirt“,“Java Mug“,“Duke Juggling Dolls“,“Java Pin“,“Java Key Chain“ ;16for (int i = 0; i prices.length; i +) out.writeDouble(pricesi);out.writeChar(t);out.writeInt(unitsi);out.writeChar(t);out.writeChars(descsi);ou

14、t.writeChar(n);out.close();/ read it in againDataInputStream in = new DataInputStream(newFileInputStream(“invoice1.txt“);double price;int unit;String desc;double total = 0.0;17try while (true) price = in.readDouble();in.readChar(); / throws out the tabunit = in.readInt();in.readChar(); / throws out

15、the tabdesc = in.readLine();System.out.println(“Youve ordered “ +unit + “ units of “ +desc + “ at $“ + price);total = total + unit * price; catch (EOFException e) System.out.println(“For a TOTAL of: $“ + total);in.close(); Youve ordered 12 units of Java T-shirt at $19.99 Youve ordered 8 units of Jav

16、a Mug at $9.99 Youve ordered 13 units of Duke Juggling Dolls at $15.99 Youve ordered 29 units of Java Pin at $3.99 Youve ordered 50 units of Java Key Chain at $4.99 For a TOTAL of: $892.88 18管道流管道用来把一个线程的输出连接到另一个线程的输入。 PipedReader/PipedInputStream实现管道的输入端; PipedWriter/PipedOutputStream实现管道的输出端。管道流模型:管道输入管道输出管道输入线程1连接线程2线程3连接管道输出19将一个线程的输出流直接挂在另一个线程的

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号