【教学课件】第七章输入输出.ppt
1,第七章 输入/输出,流式I/O基础文件随机存取文件对象输入/输出流,2,流Stream的概念,是从源到目的的字节的有序序列,先进先出。两种基本流:Input stream(输入流),Output stream(输出流),3,流操作的过程,Reading:open a streamwhile more information read informationclose the stream,Writing:open a streamwhile more information write informationclose the stream,4,两种结构的流,Node Stream:从特定源如磁盘文件或内存某区域进行读或写入。Filter Steam:使用其它的流作为输入源或输出目的地。,5,两种流类的体系,Java.io包中包含了流式I/O所需要的所有类。流式I/O类根据操作的数据类型(16位字符或字节)分成两个层次体系。,6,字节流输入流类层次,7,InputStream 方法,三个基本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()/返回标签出,8,字节流输出流类层次,9,OutputStream方法,三个基本的write()方法void write(int)/写一个字节void write(byte)/写一个字节数组void write(byte,int offset,int length)其它方法void close()void flush()/强行写,10,字符流,Reader和Writer是字符流的两个抽象超类。Reader和Writer 类实现字节和字符间的自动转换。每一个核心输入、输出流,都有相应的Reader和Writer版本。,11,Reader的类层次,12,Reader的基本方法,int read();/读单个字符int read(char cbuf);/读字符放入数组中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()/测试当前流是否准备好进行读,13,Writer的类层次,14,Writer的基本方法,int write(int c);/写单个字符int write(char cbuf);/写字符数组int write(char cbuf,int offset,int length);int write(String str);int write(String str,int offset,int length);void close()void flush()/强行写,15,字节流与字符流的比较,Reader 和 InputStream以及Writer 与 OutputStream定义的API类似,但操作的数据类型不同。所有的流InputStream、OutputStream、Reader、Writer 在创建时自动打开;程序中可以调用close方法关闭流,否则Java运行环境的垃圾收集器将隐含将流关闭。,16,17,18,19,文件流,文件流类包括:FileReader,FileWriter,FileInputStream,FileOutputStream创建文件流:常用文件名或File类的对象创建文件流。例:Copy.java,利用FileReader,FileWriter,将farrago.txt的内容拷贝到outagain.txt中。CopyBytes.java,利用FileInputStream,FileOutputStream。,20,管道流,管道用来把一个线程的输出连接到另一个线程的输入。PipedReader/PipedInputStream实现管道的输入端;PipedWriter/PipedOutputStream实现管道的输出端。管道流模型:,21,将一个线程的输出流直接挂在另一个线程的输入流,建立管道,实现线程间数据交换。PipedInputStream pin=new PipedInputStream();PipedOutputStream pout=new PipedOutputStream(pin);PipedInputStream pin=new PipedInputStream();PipedOutputStream pout=new PipedOutputStream();pin.connect(pout);或pout.connect(pin)。,管道流的创建,22,管道流示例,Rhymingwords.java,输入一组单词,先将每个单词逆序,再将所有单词排序,最后将这些单词逆序输出。程序处理流程:,23,示例中的管道流,24,是过滤流。数据从原始流成块读入或将数据积累到一个大数据块后再成批输出。基本方法:int read()int read(byte,int offset,int length)int write(int c)void write(byte,int offset,int length)BufferedReader增加readLine()方法。,BufferedInputStream/BufferedOutputStream,25,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/DataOutputStream,26,示例,/example of using inputData,27,for(int i=0;i prices.length;i+)out.writeDouble(pricesi);out.writeChar(t);out.writeInt(unitsi);out.writeChar(t);out.writeChars(descsi);out.writeChar(n);out.close();/read it in again DataInputStream in=new DataInputStream(new FileInputStream(invoice1.txt);double price;int unit;String desc;double total=0.0;,28,try while(true)price=in.readDouble();in.readChar();/throws out the tab unit=in.readInt();in.readChar();/throws out the tab desc=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.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.88,29,文件,Java.io.File 文件类提供获取文件基本信息,以及其它与文件相关的操作。创建新的文件对象:File myFile;myFile=new File(“mymotd”);myFile=new File(“”,”mymotd”);,30,文件测试与实用方法,文件名String getName()String getPath()String getAbsolutePath()String getParent()boolean renameTo(File newName)文件测试boolean exists()boolean canWrite()boolean canRead()boolean isFile()boolean isDirectory()boolean isAbsolute(),31,随机存取文件,例:从zip文件中读取特定文件,32,随机存取文件类-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”,33,对象输入/输出流,把对象保存到外存,称为永久化。实现java.io.Serializable接口类的对象可以被输入/输出。只有对象的数据被保存,方法与构造函数不被保存。以transient关键字标记的数据不被保存。Public class MyClass implements Serializable public transient Thread myThread;Private transient String customerID;private int total;,34,输出对象,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接口,35,输入对象,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();,