《输入输出》PPT课件.ppt

上传人:小飞机 文档编号:5610382 上传时间:2023-08-02 格式:PPT 页数:37 大小:293.49KB
返回 下载 相关 举报
《输入输出》PPT课件.ppt_第1页
第1页 / 共37页
《输入输出》PPT课件.ppt_第2页
第2页 / 共37页
《输入输出》PPT课件.ppt_第3页
第3页 / 共37页
《输入输出》PPT课件.ppt_第4页
第4页 / 共37页
《输入输出》PPT课件.ppt_第5页
第5页 / 共37页
点击查看更多>>
资源描述

《《输入输出》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《输入输出》PPT课件.ppt(37页珍藏版)》请在三一办公上搜索。

1、1,第7章 输入/输出,流式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(节点流

2、):从特定源如磁盘文件或内存某区域进行读或写入,它是一种最基本的流。Filter Stream(过滤流):使用其它已存在的流作为输入源或输出目的地。它要对与其相连的另一个流进行某种转换。,5,两种流类的体系,Java.io包中包含了流式I/O所需要的所有类。流式I/O类根据操作的数据类型(16位字符或字节)分成两个层次体系(字节流和字符流)。,字节流:流中的数据以8位字节为单位进行读写,以InputStream与OutputStream为基础类。字符流:流中的数据以16位字节为单位进行读写,以Reader与Writer为基础类。,Java中的流常指的是字节流。,6,两种流类的体系,InputS

3、tream和OutputStream是字节流的两个顶层父类。它们提供了输入流类和输出流类的能用API。,7,字节流输入流类层次,带阴影的类是节点流,其它类是过滤流,8,InputStream 方法,三个基本read()方法int read()/读一个字节返回int read(byte)/将数据读入byte,返回读的字节数int read(byte,int offset,int length)/将读入的数据放入一个字节数组中,并返回所读字节数。两个整形参数表示所读入数据在数组b中的存放位置。其它方法void close()/关闭流。自顶向下关闭Filter streamint available

4、()/返回未读的字节数long skip(long n)/跳过n个字节 流的回读方法 boolean markSupported()/测试打开的流是否支持书签void mark(int readlimt)/标记当前流,并建立readlimt大 小的缓冲区void reset()/返回到标签出,9,字节流输出流类层次,带阴影的类是节点流,其它类是过滤流,10,OutputStream方法,三个基本的write()方法void write(int)/写一个字节void write(byte)/写一个字节数组void write(byte,int offset,int length)其它方法void

5、 close()void flush()/强行写一般在调用close()方法关闭流前,可以先调用flush()方法,11,字符流,Reader和Writer是字符流的两个顶层抽象超类。Reader和Writer 类实现字节和字符间的自动转换。每一个核心输入、输出流,都有相应的Reader和Writer版本。,12,Reader的类层次,带阴影的类是节点流,其它类是过滤流,13,Reader的基本方法,int read();/读单个字符int read(char cbuf);/读字符放入数组中int read(char cbuf,int offset,int length);/读字符放入数组的指

6、定位置 void close()/关闭流。long skip(long n)/跳过n个字符boolean markSupported()/测试打开的流是否支持书签void mark(int)/标记当前流,并建立int大小缓冲区void reset()/返回标签出 boolean ready()/测试当前流是否准备好进行读,14,Writer的类层次,带阴影的类是节点流,其它类是过滤流,15,Writer的基本方法,int write(int c);/写单个字符int write(char cbuf);/写字符数组int write(char cbuf,int offset,int length

7、);int write(String str);int write(String str,int offset,int length);void close()/关闭流void flush()/强行写,16,字节流与字符流的比较,Reader 和 InputStream以及Writer 与 OutputStream定义的API类似,但操作的数据类型不同。所有的流InputStream、OutputStream、Reader、Writer 在创建时自动打开;程序中可以调用close方法关闭流,否则Java运行环境的垃圾收集器将隐含将流关闭。,17,18,19,20,文件流,文件流类包括:File

8、Reader,FileWriter,FileInputStream,FileOutputStream创建文件流:常用文件名或File类的对象创建文件流。例:通过文件字节流实现文件复制 CopyBytes.java,利用FileInputStream,FileOutputStream。通过文件字符流实现文件复制 Copy.java,利用FileReader,FileWriter,将farrago.txt的内容拷贝到outagain.txt中。,21,管道流,管道用来把一个线程的输出连接到另一个线程的输入。PipedReader/PipedInputStream实现管道的输入端;PipedWrit

9、er/PipedOutputStream实现管道的输出端。管道流模型:,22,将一个线程的输出流直接挂在另一个线程的输入流,建立管道,实现线程间数据交换。PipedInputStream pin=new PipedInputStream();PipedOutputStream pout=new PipedOutputStream(pin);或:PipedInputStream pin=new PipedInputStream();PipedOutputStream pout=new PipedOutputStream();pin.connect(pout);或pout.connect(pin)

10、。,管道流的创建,23,管道流示例,Rhymingwords.java,输入一组单词,先将每个单词逆序,再将所有单词排序,最后将这些单词逆序输出。程序处理流程:,24,示例中的管道流,例73,25,是过滤流。数据从原始流成块读入或将数据积累到一个大数据块后再成批输出。基本方法:int read()int read(byte,int offset,int length)int write(int c)void write(byte,int offset,int length)BufferedReader增加readLine()方法。,BufferedInputStream/BufferedOut

11、putStream,26,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(

12、long)void writeChar(char)void writeDouble(double)void writeFloat(float)void writeShort(short)void writeInt(int)void writeBytes(String)void writeChars(String),DataInputStream/DataOutputStream,数据流,27,示例,/example of using inputData,28,for(int i=0;i prices.length;i+)out.writeDouble(pricesi);out.writeCha

13、r(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;,29,try while(true)price=in.readDouble();in.readChar();/throws

14、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 or

15、dered 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,30,标准输入输出,System.in public static final InputStream inSystem.out public static final PrintStream out

16、 System.err public static final PrintStream err,示例7-5,31,文件,文件类提供获取文件基本信息,以及其它与文件相关的操作。创建新的文件对象:File myFile;myFile=new File(“mymotd”);myFile=new File(“”,”mymotd”);,32,文件测试与实用方法,文件名String getName()String getPath()String getAbsolutePath()String getParent()boolean renameTo(File newName)文件测试boolean exis

17、ts()boolean canWrite()boolean canRead()boolean isFile()boolean isDirectory()boolean isAbsolute(),33,随机存取文件,例:从zip文件中读取特定文件,34,随机存取文件类-RandomAccessFile,创建随机存取文件:myRAFile=new RandomAccessFile(String name,String mode);myRAFile=new RandomAccessFile(File file,String mode);常用的方法:数据读写方法;long getFilePointer

18、();/返回当前文件指针void seek(long pos);/文件指针定位到指定位置long length();/返回文件长度,“r”,”w”,”rw”,35,对象输入/输出流,把对象保存到外存,称为永久化。实现接口类的对象可以被输入/输出。只有对象的数据被保存,方法与构造函数不被保存。以transient关键字标记的数据不被保存。public class MyClass implements Serializable public transient Thread myThread;Private transient String customerID;private int total

19、;,36,输出对象,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 Seri

20、alizeDate();,在java.util中,实现了Serialization接口,37,输入对象,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();,

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号