第八章java的输入输出.ppt

上传人:本田雅阁 文档编号:3159702 上传时间:2019-07-17 格式: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 stream while more information read infor

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

3、 Streams) 字节流类用于向字节流读写8位二进制的字节。一般地,字节流类主要用于读写诸如图象或声音等的二进制数据。,第八章 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 oth

4、er methods: void close() int available() 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 J

5、ava用于输入输出流的类,3. Reader Methods The three basic read methods: 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) vo

6、id write( char cbuf) void write( char cbuf, int offset, int length) void write( 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. OutputStr

7、eam Class的继承关系:,第八章 java的输入输出,15,8.2 Java用于输入输出流的类,7. Reader Class的继承关系:,第八章 java的输入输出,16,8.2 Java用于输入输出流的类,8. Writer Class的继承关系:,第八章 java的输入输出,17,8.3 数据流的使用,对输入/输出流类按用途分: 数据的发起与接收流 :用于向诸如字符串、文件、管道等专用的数据池读写数据。不同的数据池由不同的类实现。 处理流:处理流类在进行读写时要执行某种处理,如缓冲、编码等,第八章 java的输入输出,18,8.3 数据流的使用,第八章 java的输入输出,19,8

8、.3 数据流的使用,1.如何使用文件流: 例题:将一个文件的内容拷贝到另一个文件。 见:copy.java,第八章 java的输入输出,20,介绍 File 类,第八章 java的输入输出,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

9、getName() String getPath() String getAbsolutePath() String getParent() boolean renameTo( File newName),File tests: boolean exists() boolean canWrite() boolean canRead() boolean isFile() boolean isDirectory(),22,介绍 File 类,第八章 java的输入输出,2. File类中的方法 General file information and utilities: long length(

10、) boolean delete() Directory utilities: boolean mkdir() String list() 见例题:File.txt,23,8.3 数据流的使用,2.如何使用管道流: 信息管道是本身是虚拟的,但为线程或程序提供了通过流进行通信的功能。,第八章 java的输入输出,进程1,进程2,24,8.3 数据流的使用,使用管道的好处:,第八章 java的输入输出,25,8.3 数据流的使用,2.如何使用管道流: 例题: 一个线程将一个字符串传递给另一个线程。 见:PipedIOApp.java,第八章 java的输入输出,26,8.4 处理流的使用 Proc

11、essing Streams,第八章 java的输入输出,27,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,1. Buffered Streams 见例题:TestBufferedStreams.java,28,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,2. 对标准输出输出的操作。 System. out :an object of PrintStream类 System. in:object of InputStream类 System. err:PrintStream 例题:从标准输入设备中读数据。

12、见:KeyboardInput.java,29,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,2. 对标准输出输出的操作。 重导向标准IO 在System类中允许我们重新定向标准输入、输出以及错误IO流。此时要用到下述简单的静态方法调用: setIn(InputStream) setOut(PrintStream) setErr(PrintStream) 见例题:Redirecting.java,30,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,3. SequenceInputStream 流。 例题:将两个

13、文件输入流合并成一个输入流。 见:SequenceIOApp.java,31,8.4 处理流的使用 Processing Streams,第八章 java的输入输出,4. DataInputStream and DataOutputStream 提供了允许从流读写任意对象与基本数据类型功能的方法。 见例题DataIOTest.java,32,8.5 文件随机读取 Random Access Files,第八章 java的输入输出,1. 创建随机文件: myRAFile = new RandomAccessFile( String name, String mode); myRAFile = n

14、ew 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备18001539号-1