第八章java的输入输出.ppt

上传人:sccc 文档编号:5301799 上传时间:2023-06-23 格式:PPT 页数:34 大小:227.02KB
返回 下载 相关 举报
第八章java的输入输出.ppt_第1页
第1页 / 共34页
第八章java的输入输出.ppt_第2页
第2页 / 共34页
第八章java的输入输出.ppt_第3页
第3页 / 共34页
第八章java的输入输出.ppt_第4页
第4页 / 共34页
第八章java的输入输出.ppt_第5页
第5页 / 共34页
点击查看更多>>
资源描述

《第八章java的输入输出.ppt》由会员分享,可在线阅读,更多相关《第八章java的输入输出.ppt(34页珍藏版)》请在三一办公上搜索。

1、1,第八章 java的输入输出,8.1 流概念8.2 Java用于输入输出流的类8.3 数据池流的使用 8.4 处理流的使用 8.5 文件随机读取,2,8.1 流概念,Java使用流的机制实现输入输出。流:是一个数据序列。有两种流:1.输入流2.输出流,第八章 java的输入输出,3,8.1 流概念,1.输入流 通过打开一个到数据源(文件、内存或网络端口上的数据)的输入流,程序可以从数据源上顺序读取数据。,第八章 java的输入输出,4,8.1 流概念,1.输入流读数据的逻辑为:open a streamwhile more informationread informationclose t

2、he stream,第八章 java的输入输出,5,8.1 流概念,2.输出流 通过打开一个到目标的输出流,程序可以向外部目标顺序写数据。,第八章 java的输入输出,6,8.1 流概念,2.输出流 写数据的逻辑为:open a streamwhile more informationwrite informationclose the stream,第八章 java的输入输出,7,8.2 Java用于输入输出流的类,按所读写的数据类型分两类:字符流类(Character Streams)字符流类用于向字符流读写16位二进制字符。字节流类(Byte Streams)字节流类用于向字节流读写8位

3、二进制的字节。一般地,字节流类主要用于读写诸如图象或声音等的二进制数据。,第八章 java的输入输出,8,8.2 Java用于输入输出流的类,java.io中的基本流类:说明:它们是抽象类,不能直接创建对象。,第八章 java的输入输出,9,8.2 Java用于输入输出流的类,1.InputStream 方法The three basic read methods:int read()int read(byte buffer)int read(byte buffer,int offset,int length)The other methods:void close()int availabl

4、e()skip(long n),第八章 java的输入输出,10,8.2 Java用于输入输出流的类,2.OutputStream 方法The three basic write methods:void write(int c)void write(byte buffer)void write(byte buffer,int offset,int length)The other methods:void close()void flush(),第八章 java的输入输出,11,8.2 Java用于输入输出流的类,3.Reader Methods The three basic read m

5、ethods:int read()int read(char cbuf)int read(char cbuf,int offset,int length)The other methods:void close()boolean ready()skip(long n),第八章 java的输入输出,12,8.2 Java用于输入输出流的类,4.Writer 方法 The three basic write methods:void write(int c)void write(char cbuf)void write(char cbuf,int offset,int length)void wr

6、ite(String string)void write(String string,int offset,int length)The other methods:void close()void flush(),第八章 java的输入输出,13,8.2 Java用于输入输出流的类,5.InputStream Class的继承关系:,第八章 java的输入输出,14,8.2 Java用于输入输出流的类,6.OutputStream Class的继承关系:,第八章 java的输入输出,15,8.2 Java用于输入输出流的类,7.Reader Class的继承关系:,第八章 java的输入输出

7、,16,8.2 Java用于输入输出流的类,8.Writer Class的继承关系:,第八章 java的输入输出,17,8.3 数据流的使用,对输入/输出流类按用途分:数据的发起与接收流:用于向诸如字符串、文件、管道等专用的数据池读写数据。不同的数据池由不同的类实现。处理流:处理流类在进行读写时要执行某种处理,如缓冲、编码等,第八章 java的输入输出,18,8.3 数据流的使用,第八章 java的输入输出,19,8.3 数据流的使用,1.如何使用文件流:例题:将一个文件的内容拷贝到另一个文件。见:copy.java,第八章 java的输入输出,20,介绍 File 类,第八章 java的输入

8、输出,File类用来访问本地文件系统中的文件和目录。1.创建File类myFile=new File(myfile.txt);File myDir=new File(MyDocs);myFile=new File(myDir,myfile.txt);,21,介绍 File 类,第八章 java的输入输出,2.File类中的方法 File names:String getName()String getPath()String getAbsolutePath()String getParent()boolean renameTo(File newName),File tests:boolean

9、exists()boolean canWrite()boolean canRead()boolean isFile()boolean isDirectory(),22,介绍 File 类,第八章 java的输入输出,2.File类中的方法 General file information and utilities:long length()boolean delete()Directory utilities:boolean mkdir()String list()见例题:File.txt,23,8.3 数据流的使用,2.如何使用管道流:信息管道是本身是虚拟的,但为线程或程序提供了通过流进行

10、通信的功能。,第八章 java的输入输出,进程1,进程2,24,8.3 数据流的使用,使用管道的好处:,第八章 java的输入输出,25,8.3 数据流的使用,2.如何使用管道流:例题:一个线程将一个字符串传递给另一个线程。见:PipedIOApp.java,第八章 java的输入输出,26,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,27,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,1.Buffered Streams见例题:TestBufferedStreams.java,28,8.4 处理流的使用 P

11、rocessing Streams,第八章 java的输入输出,2.对标准输出输出的操作。System.out:an object of PrintStream类System.in:object of InputStream类System.err:PrintStream例题:从标准输入设备中读数据。见:KeyboardInput.java,29,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,2.对标准输出输出的操作。重导向标准IO在System类中允许我们重新定向标准输入、输出以及错误IO流。此时要用到下述简单的静态方法调用:setIn(InputSt

12、ream)setOut(PrintStream)setErr(PrintStream)见例题:Redirecting.java,30,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,3.SequenceInputStream 流。例题:将两个文件输入流合并成一个输入流。见:SequenceIOApp.java,31,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,4.DataInputStream and DataOutputStream 提供了允许从流读写任意对象与基本数据类型功能的方法。见例题DataIOTes

13、t.java,32,8.5 文件随机读取Random Access Files,第八章 java的输入输出,1.创建随机文件:myRAFile=new RandomAccessFile(String name,String mode);myRAFile=new RandomAccessFile(File file,String mode);例如:new RandomAccessFile(farrago.txt,r);new RandomAccessFile(farrago.txt,rw);,33,8.5 文件随机读取Random Access Files,第八章 java的输入输出,2.常用方法:getFilePointer()seek(long pos)length()skipBytes(int n)见例题:RandomIOApp.java,34,综合例题,见例题IODemo.java,第八章 java的输入输出,作业:第八章作业,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号