教学课件第七章输入输出

上传人:沈*** 文档编号:228288362 上传时间:2023-08-21 格式:PPT 页数:35 大小:407.97KB
返回 下载 相关 举报
教学课件第七章输入输出_第1页
第1页 / 共35页
教学课件第七章输入输出_第2页
第2页 / 共35页
教学课件第七章输入输出_第3页
第3页 / 共35页
点击查看更多>>
资源描述
第七章第七章 输入输入/输出输出流式流式I/O基础基础文件文件随机存取文件随机存取文件对象输入对象输入/输出流输出流1流流Stream的概念的概念是从源到目的的字节的有序序列,先进先出。是从源到目的的字节的有序序列,先进先出。两种基本流:两种基本流:Input stream(输入流)(输入流),Output stream(输出流)(输出流)2流操作的过程流操作的过程Reading:open a streamwhile more information read informationclose the stream Writing:open a streamwhile more information write informationclose the stream3两种结构的流两种结构的流 Node Stream:从特定源如磁盘文件或内存某:从特定源如磁盘文件或内存某区域进行读或写入。区域进行读或写入。Filter Steam:使用其它的流:使用其它的流作为输入源或输出作为输入源或输出目的地。目的地。l l e HoNode InputStreamFilter InputStreamread()4两种流类的体系两种流类的体系 Java.io包中包含了流式包中包含了流式I/O所需要的所有类。所需要的所有类。流式流式I/O类根据操作的数据类型(类根据操作的数据类型(16位字符或字位字符或字节)分成两个层次体系。节)分成两个层次体系。5字节流字节流输入流类层次输入流类层次6InputStream 方法方法三个基本三个基本read()方法方法int read()/读一个字节返回读一个字节返回int read(byte )/将数据读入将数据读入byte,返回读的字节数返回读的字节数int read(byte,int offset,int length)其它方法其它方法void close()/关闭流。自顶向下关闭关闭流。自顶向下关闭Filter streamint available()/返回未读的字节数返回未读的字节数long skip(long n)/跳过跳过n个字节个字节boolean markSupported()/测试打开的流是否支持书签测试打开的流是否支持书签void mark(int)/标记当前流,并建立标记当前流,并建立int大小缓冲区大小缓冲区void reset()/返回标签出返回标签出 7字节流字节流输出流类层次输出流类层次8OutputStream方法方法三个基本的三个基本的write()方法方法void write(int)/写一个字节写一个字节void write(byte)/写一个字节数组写一个字节数组void write(byte,int offset,int length)其它方法其它方法void close()void flush()/强行写强行写9字符流字符流 Reader和和Writer是字符流的两个抽象超是字符流的两个抽象超类。类。Reader和和Writer 类实现字节和字符间的自类实现字节和字符间的自动转换。动转换。每一个核心输入、输出流,都有相应的每一个核心输入、输出流,都有相应的Reader和和Writer版本。版本。10Reader的类层次的类层次11Reader的基本方法的基本方法int read()int read();/读单个字符读单个字符int read(char cbuf)int read(char cbuf);/读字符放入数组中读字符放入数组中int read(char cbuf,int offset,int length)int read(char cbuf,int offset,int length);/读字符放入数组的指定位置读字符放入数组的指定位置 void close()/关闭流。关闭流。long skip(long n)/跳过跳过n个字符个字符boolean markSupported()/测试打开的流是否支持书签测试打开的流是否支持书签void mark(int)/标记当前流,并建立标记当前流,并建立int大小缓冲区大小缓冲区void reset()/返回标签出返回标签出 boolean ready()/测试当前流是否准备好进行读测试当前流是否准备好进行读12Writer的类层次的类层次13Writer的基本方法的基本方法int write(int c);/int write(int c);/写单个字符写单个字符int write(char cbuf);/int write(char cbuf);/写字符数组写字符数组int write(char cbuf,int offset,int length);int write(char cbuf,int offset,int length);int write(String str);int write(String str);int write(String str,int offset,int length);int write(String str,int offset,int length);void close()void flush()/强行写强行写14字节流与字符流的比较字节流与字符流的比较 Reader Reader 和和 InputStreamInputStream以及以及Writer Writer 与与 OutputStream定定义的义的API类似,但操作的数据类型不同。类似,但操作的数据类型不同。所有的流所有的流InputStreamInputStream、OutputStream 、ReaderReader、Writer Writer 在创建时自动打开;程序中可以调用在创建时自动打开;程序中可以调用closeclose方法关闭方法关闭流,否则流,否则JavaJava运行环境的垃圾收集器将隐含将流关闭。运行环境的垃圾收集器将隐含将流关闭。15I/O Streams Type of I/OType of I/OStreamsStreamsDescriptionDescriptionMemory CharArrayReaderCharArrayReaderCharArrayWriterCharArrayWriterByteArrayInputStreamByteArrayInputStreamByteArrayOutputStreamByteArrayOutputStream 从从/向内存数组读写数据向内存数组读写数据.StringReaderStringReaderStringWriterStringWriterStringBufferInputStreamStringBufferInputStream 从从/向内存字符串读写数据向内存字符串读写数据 Pipe PipedReaderPipedReaderPipedWriterPipedWriterPipedInputStreamPipedInputStreamPipedOutputStreamPipedOutputStream 实现管道的输入和输出实现管道的输入和输出 File FileReaderFileReaderFileWriterFileWriterFileInputStreamFileInputStreamFileOutputStreamFileOutputStream 统称为文件流。对文件进行统称为文件流。对文件进行读、写操作读、写操作 16I/O Streams Type of I/OType of I/OStreamsStreamsDescriptionDescriptionObjectSerialization ObjectInputStreamObjectInputStreamObjectOutputStreamObjectOutputStream 对象的输入、输出对象的输入、输出 DataConversion DataInputStreamDataInputStreamDataOutputStreamDataOutputStream 读、写基本数据类型读、写基本数据类型 Printing PrintWriterPrintWriterPrintStreamPrintStream 包含方便的打印方法包含方便的打印方法 Buffering BufferedReaderBufferedReaderBufferedWriterBufferedWriterBufferedInputStreamBufferedInputStreamBufferedOutputStreamBufferedOutputStream 在读入或写出时,对数据进在读入或写出时,对数据进行缓存,以减少行缓存,以减少I/O的次数。的次数。17I/O Streams Type of I/OType of I/OStreamsStreamsDescriptionDescriptionFiltering FilterReaderFilterReaderFilterWriterFilterWriterFilterInputStreamFilterInputStreamFilterOutputStreamFilterOutputStream 过滤流过滤流,在数据进行读或写时,在数据进行读或写时进行过滤。进行过滤。Concatenation SequenceInputStreamSequenceInputStream 把多个输入流连接成一个输入把多个输入流连接成一个输入流流 Counting LineNumberReaderLineNumberReaderLineNumberInputStreamLineNumberInputStream 在读入数据时对行记数在读入数据时对行记数 Peeking Ahead PushbackReaderPushbackReaderPushbackInputStreamPushbackInputStream 通过缓存机制,进行预读通过缓存机制,进行预读 Converting between Bytes and Characters InputStreamReaderInputStreamReaderOutputStreamWriterOutputStreamWriter 按照一定的编码按照一定的编码/解码标准将字解码标准将字节流转换为字符流,或进行反节流转换为字符流,或进行反向转换。向转换。18文件流文件流文件流类包括:文件流类包括:FileReader,FileWriter,FileInputStream,FileOutputStream创建文件流:常用文件名或创建文件流:常用文件名或File类的对象创建文件流。类的对象创建文件流。例:例:Copy.java,利用利用FileReader,FileWriter,将将farrago.txt的内容拷贝到的内容拷贝到outagain.txt中。中。CopyBytes.java,利用,利用FileInputStream,FileOutputStream。19管道流管道流管道用来把一个线程的输出连接到另一个线程的输入。管道用来把一个线程的输出连接到另一个线程的输入。PipedReader/PipedInputStream实现管道的输入端;实现管道的输入端;PipedWriter/PipedOutputStream实现管道的输出端。实现管道的输出端。管道流模型:管道流模型:管道输入管道输出管道输入线程1连接线程2线程3连接管道输出20将一个线程的输出流直接挂在另一个线程的输入流,将一个线程的输出流直接挂在另一个线程的输入流,建立管道,实现线程间数据交换。建立管道,实现线程间数据交换。PipedInputStream pin=new PipedInputStream();PipedOutputStream pout=new PipedOutputStream(pin);PipedInputStream pin=new PipedInputStream();PipedOutputStream pout=new PipedOutputStream();pin.connect(pout);或或pout.connect(pin)。管道流的创建管道流的创建21管道流示例管道流示例Rhymingwords.java,输入一组单词,先将每个单词逆序,输入一组单词,先将每个单词逆序,再将所有单词排序,最后将这些单词逆序输出。再将所有单词排序,最后将这些单词逆序输出。程序处理流程:程序处理流程:22示例中的管道流示例中的管道流23是过滤流。是过滤流。数据从原始流成块读入或将数据积累到数据从原始流成块读入或将数据积累到一个大数据块后再成批输出。一个大数据块后再成批输出。基本方法:基本方法:int read()int read(byte,int offset,int length)int write(int c)int write(int c)void write(byte,int offset,int length)BufferedReader增加增加readLine()方法。方法。BufferedInputStream/BufferedOutputStream24 DataInputStream和和DataOutputStream(Filter stream)读写基本数据类型:读写基本数据类型:DataInputStream方法方法 byte readByte()boolean readBoolean()long readLong()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 writeBytes(String)void writeChars(String)DataInputStream/DataOutputStream25示例示例/example of using inputData&outputData/example of using inputData&outputData/DataIOTeat.java/DataIOTeat.javaimport java.io.*;import java.io.*;public class DataIOTest public class DataIOTest public static void main(String args)throws IOException public static void main(String args)throws IOException /write the data out /write the data out DataOutputStream out=new DataOutputStream(new DataOutputStream out=new DataOutputStream(new FileOutputStream(invoice1.txt);FileOutputStream(invoice1.txt);double prices=19.99,9.99,15.99,3.99,4.99;double prices=19.99,9.99,15.99,3.99,4.99;int units=12,8,13,29,50;int units=12,8,13,29,50;String descs=Java T-shirt,String descs=Java T-shirt,Java Mug,Java Mug,Duke Juggling Dolls,Duke Juggling Dolls,Java Pin,Java Pin,Java Key Chain;Java Key Chain;26 for(int i=0;i prices.length;i+)for(int i=0;i prices.length;i+)out.writeDouble(pricesi);out.writeDouble(pricesi);out.writeChar(t);out.writeChar(t);out.writeInt(unitsi);out.writeInt(unitsi);out.writeChar(t);out.writeChar(t);out.writeChars(descsi);out.writeChars(descsi);out.writeChar(n);out.writeChar(n);out.close();out.close();/read it in again /read it in again DataInputStream in=new DataInputStream(new DataInputStream in=new DataInputStream(new FileInputStream(invoice1.txt);FileInputStream(invoice1.txt);double price;double price;int unit;int unit;String desc;String desc;double total=0.0;double total=0.0;27try try while(true)while(true)price=in.readDouble();price=in.readDouble();in.readChar();/throws out the tab in.readChar();/throws out the tab unit=in.readInt();unit=in.readInt();in.readChar();/throws out the tab in.readChar();/throws out the tab desc=in.readLine();desc=in.readLine();System.out.println(Youve ordered +System.out.println(Youve ordered +unit+units of +unit+units of +desc+at$+price);desc+at$+price);total=total+unit*price;total=total+unit*price;catch(EOFException e)catch(EOFException e)System.out.println(For a TOTAL of:$+total);System.out.println(For a TOTAL of:$+total);in.close();in.close();Youve ordered 12 units of Java T-shirt at$19.99Youve ordered 8 units of Java Mug at$9.99Youve ordered 13 units of Duke Juggling Dolls at$15.99Youve ordered 29 units of Java Pin at$3.99Youve ordered 50 units of Java Key Chain at$4.99For a TOTAL of:$892.8828文件文件 Java.io.File 文件类提供获取文件基本信息,以及文件类提供获取文件基本信息,以及其它与文件相关的操作。其它与文件相关的操作。创建新的文件对象:创建新的文件对象:File myFile;myFile=new File(“mymotd”);myFile=new File(“”,”mymotd”);29文件测试与实用方法文件测试与实用方法文件名文件名String getName()String getPath()String getAbsolutePath()String getParent()boolean renameTo(File newName)文件测试文件测试boolean exists()boolean canWrite()boolean canRead()boolean isFile()boolean isDirectory()boolean isAbsolute()30随机存取文件随机存取文件例:从例:从zip文件中读取特定文件文件中读取特定文件31随机存取文件类随机存取文件类-RandomAccessFile创建随机存取文件:创建随机存取文件:myRAFile=new RandomAccessFile(String name,String mode);myRAFile=new RandomAccessFile(File file,String mode);常用的方法:常用的方法:数据读写方法;数据读写方法;long getFilePointer();/返回当前文件指针返回当前文件指针void seek(long pos);/文件指针定位到指定位置文件指针定位到指定位置long length();/返回文件长度返回文件长度“r”,”w”,”rw”32对象输入对象输入/输出流输出流把对象保存到外存,称为永久化。把对象保存到外存,称为永久化。实现实现java.io.Serializable接口类的对象可以被输入接口类的对象可以被输入/输出。输出。只有对象的数据被保存,方法与构造函数不被保存。只有对象的数据被保存,方法与构造函数不被保存。以以transient关键字标记的数据不被保存。关键字标记的数据不被保存。Public class MyClass implements Serializable public transient Thread myThread;Private transient String customerID;private int total;33输出对象输出对象Public class SerializeDate SerializeDate()Date d=new Date();try FileOutputStream f=new FileOutputStream(“date.ser”);ObjectOutputStream s=new ObjectOutputStream(f);s.writeObject(d);f.close();catch(IOException e)e.printStackTrace();public static void main(String args)SerializeDate b=new SerializeDate();在在java.util中,中,实现了实现了Serialization接口接口34输入对象输入对象Public class UnSerializeDate UnSerializeDate()Date d=null;try FileInputStream f=new FileInputStream(“date.ser”);ObjectInputStream s=new ObjectInputStream(f);d=(Date)s.readObject();f.close();catch(Exception e)e.printStackTrace();public static void main(String args)UnSerializeDate a=new UnSerializeDate();35
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!