大纲第十五章输入输出

上传人:E**** 文档编号:91495315 上传时间:2019-06-28 格式:PPT 页数:42 大小:311.50KB
返回 下载 相关 举报
大纲第十五章输入输出_第1页
第1页 / 共42页
大纲第十五章输入输出_第2页
第2页 / 共42页
大纲第十五章输入输出_第3页
第3页 / 共42页
大纲第十五章输入输出_第4页
第4页 / 共42页
大纲第十五章输入输出_第5页
第5页 / 共42页
点击查看更多>>
资源描述

《大纲第十五章输入输出》由会员分享,可在线阅读,更多相关《大纲第十五章输入输出(42页珍藏版)》请在金锄头文库上搜索。

1、课程内容:流类、外部文件处理、数据流、打印流、JFileChooser、缓冲流、文本输入输出、随机文件处理 授课时间:2006/06/08 教学目标:了解和掌握流式输入输出与文件处理 重点:流式输入输出,文件处理等 教学方法:讲授 教学过程:(省略),第十五章 输入输出,数据流,数据流是连续单路流动数据的抽象,数据流的类,数据流类分为两种类型:字节流和字符流 InputStream/OutputStream 类是所有字节流类的基类,Reader/Writer 类是所有字符流的基类。 InputStream/ OutputStream 的子类与Reader/Writer 的子类非常相似,字节流类

2、,字符流类,InputStream类,abstract int read() throws IOException int read(byte b) throws IOException void close() throws IOException void available() throws IOException void skip() throws IOException,Reader类,Reader类与InputStream类似。 在Reader类中的方法处理的是字符 abstract int read() throws IOException int read(char b) t

3、hrows IOException void close() throws IOException void skip() throws IOException,OutputStream类,abstract void write(int b) throws IOException void write(byte b) throws IOException void close() throws IOException void flush() throws IOException,Writer类,abstract void write(int b) throws IOException voi

4、d write(char b) throws IOException void close() throws IOException void flush() throws IOException,外部文件处理,必须使用文件流来读写磁盘文件。使用FileInputStream 或 FileOutputStream 处理字节流, FileReader 或 FileWriter处理字符流,文件I/O流类的构造函数,通过输入输出文件创建 FileInputStream, FileOutputStream, FileReader, 和FileWriter 实例: FileInputStream inf

5、ile = new FileInputStream(“in.dat“); FileOutputStream outfile = new FileOutputStream(“out.dat“); FileReader infile = new FileReader(“in.dat“); FileWriter outfile = new FileWriter(“out.dat“);,范例:处理外部文件,CopyFileUsingByteStream,数据流,数据流(DataInputStream 和 DataOutputStream)以与机器无关的方式读取和写入Java的基本数据类型数据,即可以在

6、一台机器上写一个数据文件,到另一台不同操作系统或不同文件结构的机器上读取该文件,DataInputStream类 的方法,int readByte() throws IOException int readShort() throws IOException int readInt() throws IOException int readLong() throws IOException float readFloat() throws IOException double readDouble() throws IOException char readChar() throws IOEx

7、ception boolean readBoolean() throws IOException String readUTF() throws IOException,void writeByte(byte b) throws IOException void writeShort(short s) throws IOException void writeInt(int i) throws IOException void writeLong(long l) throws IOException void writeFloat(float f) throws IOException voi

8、d writeDouble(double d) throws IOException void writeChar(char c) throws IOException void writeBoolean(boolean b) throws IOException void writeBytes(String l) throws IOException void writeChars(String l) throws IOException void writeUTF(String l) throws IOException,DataOutputStream类 的方法,数据流构造函数,Data

9、InputStream infile = new DataInputStream(new FileInputStream(“in.dat“); 创建输入文件 in.dat. DataOutputStream outfile = new DataOutputStream(new FileOutputStream(“out.dat“); 创建输出文件 out.dat.,范例:使用数据流,创建10个随机整数,存入一个数据文件,再从文件中读取数据,在控制台上显示出来 TestDataStreams,Print Streams,数据输出流输出数据的二进制表示,不能以文本形式浏览其内容。在Java中可以使

10、用打印数据流(print streams)把数据输出到文件中,这样的文件可以以文本的格式浏览。 PrintStream 和PrintWriter 类提供了这个功能,PrintWriter(Writer out) PrintWriter(Writer out, boolean autoFlush) PrintWriter(OutputStream out) PrintWriter(OutputStream out, boolean autoFlush),PrintWriter 构造函数,void print(Object o) void print(String s) void println(

11、String s) void print(char c) void print(char cArray) void print(int i) void print(long l) void print(float f) void print(double d) void print(boolean b),PrintWriter 的方法,创建10个随机整数并把它们存入文本文件,可以使用操作系统命令查看该文件内容 TestPrintWriters,范例:使用打印数据流,缓冲数据流,Java 引入缓冲数据流(buffered streams),通过减少读写次数加快输入输出速度。输入时,数组成块读入数

12、据而不是一个字节一个字节读入;输出时,数据先缓存在缓冲区内,然后一起写到文件中。 强烈推荐使用缓冲数据流,缓冲数据流的构造函数,BufferedInputStream (InputStream in) BufferedInputStream (InputStream in, int bufferSize) BufferedOutputStream (OutputStream in) BufferedOutputStream (OutputStream in, int bufferSize) BufferedReader(Reader in) BufferedReader(Reader in,

13、int bufferSize) BufferedWriter(Writer out) BufferedWriter(Writer out, int bufferSize),范例:在文本区显示文件,用户在文本框里输入文件名,点击View按钮,文件内容就在文本区显示出来 ViewFile,范例:使用文件对话框,使用JFileChooser 创建一个简单的记事本用来打开和保存文件。记事本允许用户打开一个已经存在的文件,编辑文件并保存在当前文件中或保存到一个指定的文件中。可在文本区显示和编辑文件 FileDialogDemo,交互式输入输出有两种类型。一种是从键盘进行简单的输入和以纯文本形式进行简单的

14、输出。另一种是在图形环境下(如frames和 applets)进行输入输出。前者称为文本交互式输入输出(text interactive I/O) ,后者称为图形交互式输入输出( graphical interactive I/O),控制台文本输入输出,控制台文本输入输出(cont.),要进行控制台输出,可以对PrintStream使用 System.out的任何一个方法。但是Java不支持键盘输入。要从键盘输入,首先利用下面语句从键盘读取一个字符串 MyInput,对象数据流,对象流(Object streams)允许在对象层次进行输入输出 为了能够读写对象,定义对象的类必须实现: java

15、.io.Serializable 或 java.io.Serializable 或 java.io.Externalizable 接口,Serializable接口,Serializable接口是个标记性接口。没有方法,所以实现 Serializable接口不需要在类中添加额外ide 代码 实现这个接口中可以启动Java的序列化机制,自动执行存储对象和数组的过程,ObjectOutputStream 类用来存储对象,类ObjectInputStream 用来恢复对象 这两个类是建立在其他几个类的基础上的,对象数据流(cont.),ObjectOutput 和 ObjectInput,范例:测试

16、对象数据流,存储 MessagePanel 和 Date 对象,并恢复这些对象 ObjectStreamDemo,随机读写文件,Java 提供了RandomAccessFile 类,允许同时对文件进行读写 RandomAccessFile 类扩展了 Object 类,并实现了DataInput 和 DataOutput 接口,RandomAccessFile类的方法,RandomAccessFile中许多方法与DataInputStream和DataOutputStream中的相同。例如, readInt(), readLong(), writeDouble(), readLine(), writeInt(), writeLong()等,它们既可以在数据输入或数据输出流中使用,也可以在数据流RandomAccessFile 中使用,RandomAccessFile类的方法(cont.),long length()IOException 返回文件长度 final void writeChar(int v) throws IOException 把一个字

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

当前位置:首页 > 高等教育 > 大学课件

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