分享
分销 收藏 举报 申诉 / 40

类型输入输出数据流(用).ppt

  • 上传人:仙人****88
  • 文档编号:14772274
  • 上传时间:2026-11-19
  • 格式:PPT
  • 页数:40
  • 大小:1.28MB
  • 下载积分:10 金币
  • 配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    输入输出 数据流
    资源描述:
    单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,单击此处编辑母版标题样式,*,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,第,1,0章,输入输出数据流,流的概念,流的分类,流的使用(以字节流为例),标准输入,/,输出流,文件输入,/,输出流,过滤流,字符流的使用,1,流(,Stream,)的概念,流,是从,源,到,目的地,的,有序字节序列,,具有,先进先出,的特征。,根据流与程序的关系将流分为,输入流,和,输出流,两类。,程序从输入流,读取,数据;向输出流,写出,数据。,2,流的概念,源,输入流的源可以是文件、标准输入(键盘)、其他外部输入设备或者其他输入流。,目的地,输出流的目的地可以是文件、标准输出(显示器)、其他外部输出设备或者其他输出流。,Java,中输入输出是通过流来实现的。相关的类都在,java.io,包中。,3,流的分类,输入流,/,输出流,按流与程序的关系分。如前所述。,字节流,/,字符流,按流中处理的数据是以字节(,8,位)为单位还是以字符(,16,位)为单位分为字节流和字符流。,Java,中字节流和字符流分属两个不同的体系。,节点流,/,过滤流,按流与原始数据载体(文件,设备)的关系分为节点流和过滤流。,4,流的分类(续),节点流,(Node Stream),:直接与原始数据存在的特定介质(如磁盘文件或其他外部设备、内存某区域或其他程序)打交道的流,在流的序列中离程序最远。,过滤流,(Filter Stream),:使用其它的流作为输入源或输出目的地,对流中的数据提供进一步处理的流。其他的流可以是节点流,也可以是另一种过滤流。,过滤流不能单独使用,。,一个输入流链或输出流链中一定,有且只有一个节点流,;可以没有,也可以有多个过滤流。,5,字节流的层次结构,过滤流,结点流,抽象类,6,字符流(读写器)的类层次结构,CharArrayReader,StringReader,PipedReader,InputStreamReader,BufferedReader,FilterReader,FileReader,LineNumberReader,PushbackReader,Reader,CharArrayWriter,StringWriter,PipedWriter,OutputStreamWriter,BufferedWriter,FilterWriter,FileWriter,Writer,Object,PrinterWriter,7,InputStream,类的常用方法,读一个字节,并返回该字节。未读到返回,-1,public,int,read()throws,IOException,关闭流,public void close()throws,IOException,将数据读入字节数组,b,返回所读的字节数,int,read(byte,b)throws,IOException,将数据读入字节数组,b,返回所读的字节数,,offset,和,length,指示,byte,中存放读入字节的位置。,int,read(byte b,int,offset,int,length)throws,IOException,8,OutputStream,的常用方法,写一个字节,void write(,int,)throws,IOException,关闭输出流,void close()throws,IOException,强行将缓冲区的数据写到目的地。,void flush()throws,IOException,写一个字节数组,void,write(byte,b)throws,IOException,void,write(byte,b,int,offset,int,length)throws,IOException,9,标准输入输出流和错误流,System.out,:,把输出送到缺省的显示,(,通常是显示器,),System.in,从标准输入获取输入,(,通常是键盘,),System.err,把错误信息送到缺省的显示,System,是,final,类,,in,out,err,是,System,的静态成员变量,因此可以用,System.in,等形式直接使用。,out,的用法大家已熟知了,,err,的用法与,out,一样。,10,标准输入,System.in,在,System,中,in,的完整定义是:,public static final,InputStream,in;,in,的主要方法,:,public,int,read()throws,IOException,public,int,read(byte,b)throws,IOException,使用注意事项:,前者返回读入的一字节的数据,但返回的是,int,整型值,取值范围是,0-255,。,后者返回读入的字节数,读入的各字节保存在作为参数的字节型数组对象中。,执行,read,时,程序会等待用户的输入。输入完成后再接着执行后面的语句。,11,流的使用过程,输入,/,输出流的使用过程,:,实例化一个输入,/,输出流对象,使用该输入,/,输出流对象的方法读入,/,写出数据,关闭该输入,/,输出流对象,注意事项,输入,/,输出流的方法会抛出异常,因此必须进行异常处理。,标准输入,/,输出,/,错误流对象,System.in,,,System.out,、,System.err,始终存在,不需要实例化,也不需要关闭。,12,例:使用,System.in,实现键盘数据输入,import,java.io,.*;,public class,TestInput,public static void,main(String,args,),try,byte,bArray,=new byte128;,System.out.println,(,请输入一些东西:,);,System.in.read(bArray,);,String s=new String(bArray,0,bArray.length).trim();,System.out.println,(,你输入的是:,+s);,catch(IOException,ioe,),System.out.println(ioe.toString,();,13,学习任务:,学会如何对文件进行读写操作,任务:编写一个程序,统计一个磁盘文本文件中的单词数。,14,从文件读数据:,FileInputStream,InputStream,是抽象类,不能直接用,new,InputStream,(),构造实例。,FileInputStream,是,InputStream,的子类,可以生成实例。,FileInputStream,有三个构造方法,最常用的构造方法如下:,Public,FileInputStream(String,fileName,)throws,FileNotFoundException,Public,FileInputStream(File,file)throws,FileNotFoundException,fileName,用来指定输入文件名及其路径,,file,是一个,File,对象,15,例:,读出一个文本文件的内容并显示,import,java.io,.*;,class,ReadFromFile,public static void,main(String,args,),InputStream,in;/,FileInputStream,in;,这样声明也可以,try,in=new,FileInputStream(test.txt,);,int,aByte,;,aByte,=,in.read,();,while(,aByte,!=-1),System.out.print(char)aByte,);,aByte,=,in.read,();,in.close,();,catch(FileNotFoundException,e)System.out.println,(,当前目录下文件,test.txt,不存在!,);,catch(IOException,e)System.out.println,(,发生输入输出错误!,);,16,向文件写数据:,FileOutputStream,OutputStream,是抽象类,不能直接用,new,OutputStream,(),构造实例。,FileOutputStream,是,OutputStream,的子类,可以生成实例。,FileOutputStream,有,5,个构造方法,最常用的构造方法如下:,Public,FileOutputStream(String,name)throws,FileNotFoundException,和,Public,FileOutputStream(String,name,boolean,append)throws,FileNotFoundException,Name,用来指定输入文件名及其路径,,append,为,true,时数据将添加到文件已有内容的末尾。,17,例题:,使用,FileOutputStream,实现文件复制。,import,java.io,.*;,class,CopyAFile,public static void,main(String,args,),InputStream,in;,OutputStream,out;,try,in=new,FileInputStream(test.txt,);,out=new,FileOutputStream(copyResult.txt,);,/out=new,FileOutputStream(copyResult.txt,true,);,int,aByte,;,aByte,=,in.read,();,while(,aByte,!=-1),out.write(aByte,);,aByte,=,in.read,();,in.close,();,out.close,();,System.out.println,(,文件复制完毕。,test.txt,已经复制到,copyResult.txt,中。,);,catch(FileNotFoundException,e),System.out.println,(,当前目录下文件,test.txt,不存在!,);,catch(IOException,e),System.out.println,(,发生输入输出错误!,);,18,学习任务:,学会使用过滤流(缓冲流,数据流等),任务:,使用缓冲流(,BufferedInputStream,和,BufferedOutputStream,),改写前面的文件读写程序。,编写一个程序,将,10,个整型数据写入文件,再读出来求和。,19,BufferedInputStream,是过滤流。数据流从原始流成块读入数据,放在一个内部字节数组中。通过减少,IO,次数提高效率。,构造方法,BufferedInputStream(InputStream,in),BufferedInputStream(InputStream,in,int,buffersize,),基本方法:,int,read()throws,IOException,int,read(,byte,int,offset,int,length,),throws,IOException,void close()throws,IOException,20,BufferedOutputStream,是过滤流。将数据积累到一个大数据块后再成批输出。通过减少,IO,次数提高效率。,构造方法:,BufferedOutputStream(OutputStream,out),BufferedOutputStream(OutputStream,out,int,buffersize,),基本方法:,void,write(int,c)throws,IOException,void,write(byte,int,offset,int,length)throws,IOException,void flush()throws,IOException,void close()throws,IOException,21,输入,/,输出流的套接,通过流的套接,可以利用各种流的特性共同处理数据。,如:,数据源,程序,FileInpuStream,读入,BufferedInputStream,提高效率,DataInputStream,读取基本数据类型,数据源,程序,FileOutpuStream,写出,BufferedOutputStream,提高效率,DataOutputStream,写出基本数据类型,注意:直接对数据源读写的是节点流,不能是过滤流。过滤流通常必须套接节点流。,22,流的套接举例:,FileInputStream,套接,BufferedInputStream,import,java.io,.*;,class,TestBufferedInput,public static void,main(String,args,)throws,IOException,InputStream,in;,in=new,BufferedInputStream(new,FileInputStream(test.txt,);,int,aByte,;,aByte,=,in.read,();,while(,aByte,!=-1),System.out.print(char)aByte,);,aByte,=,in.read,();,in.close,();,23,属过滤流。能按,java,的基本数据类型读写流中的数据:,DataInputStream,方法,byte,readByte,(),boolean,readBoolean,(),long,readLong,()char,readChar,(),double,readDouble,()float,readFloat,(),short,readshort,(),int,readInt,(),String,readUTF,()/,读取以,UTF,格式保存的字符串,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,),void,WriteUTF(String,str,)/,将字符串以,UTF,格式写出,DataInputStream/DataOutputStream,24,过滤流示例,/,使用,DataOutputStream,将一些数据写入文件,再用,DataInputStream,读入,/,DataIOTeat.java,import,java.io,.*;,public class,DataIOTest,public static void main(String,args,)throws,IOException,/,注意也套接了节点流,FileOutputStream,DataOutputStream,out=new,DataOutputStream(new,FileOutputStream(invoice1.txt);,double prices=19.99,9.99,15.99,3.99,4.99;,int,units=12,8,13,29,50;,String,descs,=Java T-shirt,Java Mug,Duke Juggling Dolls,Java Pin,Java Key Chain;,25,过滤流示例,过滤流示例,/,使用,DataOutputStream,将一些数据写入文件,再用,DataInputStream,读入,/,DataIOTeat.java,import,java.io,.*;,public class,DataIOTest,public static void main(String,args,)throws,IOException,/,注意也套接了节点流,FileOutputStream,DataOutputStream,out=new,DataOutputStream(new,FileOutputStream(invoice1.txt);,double prices=19.99,9.99,15.99,3.99,4.99;,int,units=12,8,13,29,50;,String,descs,=Java T-shirt,Java Mug,Duke Juggling Dolls,Java Pin,Java Key Chain;,26,for(,int,i=0;i prices.length;i+),out.writeDouble(pricesi,);,out.writeChar(t,);,out.writeInt(unitsi,);,out.writeChar(t,);,out.write,UTF,(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;,27,for(,int,i=0;i,prices.length,;i+),price=,in.readDouble,();,in.readChar,();/,读入,tab,键,unit=,in.readInt,();,in.readChar,();/,读入,tab,键,desc,=,in.readUTF,();,in.readChar,();/,读入,tab,键,System.out.println(Youve,ordered +,unit+units of +,desc,+at$+price);,total=,total,+unit*price;,System.out.println(For,a TOTAL of:$+total);,in.close,();,28,字符流,Reader,和,Writer,是两个字符流的顶层抽象类。定义了读写,16,位字符的通用,API,。能够处理,Unicode,的所有字符。,Reader,和,Writer,类实现字节和字符间的自动转换。如果是读写文本类的数据使用字符流更佳。,29,字符流的常用类,输入流,BufferedReader,public String,readLine,()throws,IOException,InputStreamReader,FileReader,输出流,BufferedWriter,OutputStreamWriter,FileWriter,30,使用字符输入流从键盘输入,import,java.io,.*;,public class,StandardIO,public static void,main(String,args,),String s;,BufferedReader,in=new,BufferedReader(new,InputStreamReader(System.in,);,System.out.println(Please,input:);,try,s=,in.readLine,();,while(!s.equals(exit,),System.out.println,(read:+s);,s=,in.readLine,();,System.out.println(End,of,Inputing,.);,in.close,();,catch(IOException,e),e.printStackTrace,();,31,文件的处理,File,类提供了获得文件基本信息及操作文件的一些工具,创建一个新的,File,对象,File myFile;,myFile=new File(mymotd);,myFile=new File(/,,,mymotd);,File myDir=new File(/);,myFile=new Fle(myDir,,,mymotd);,32,与文件名相关的方法,String getName(),获取文件名,String getPath(),获取文件路径,String getAbsolutePath(),获取文件绝对路径,String getParent(),获取文件父目录名称,boolean renameTo(File newName),更改文件名,成功则返回,true,,,否则返回,false,33,文件测定方法,boolean exists(),文件对象是否存在,boolean canWrite(),文件对象是否可写,boolean canRead(),文件对象是否可读,boolean isFile(),文件对象是否是文件,boolean isDirectory(),文件对象是否是目录,boolean isAbsolute(),文件对象是否是绝对路径,34,常用文件信息和方法,long lastModified(),获取文件最后修改时间,long length(),获取文件长度,boolean delete(),删除文件对象指向的文件,成功则返回,true,,,否则返回,false,35,程序,10-10,以上方法的使用见,程序,10-10,36,目录工具,boolean mkdir(),创建新目录,boolean mkdirs(),创建新目录,mkdir(),和,mkdirs(),区别在于用,mkdirs(),可以一次生成多个层次的子目录,String list(),列出符合模式的文件名,类,File,同样可以用来描述一个目录,对其进行的操作也与文件相同,只是对目录不能改变目录名,也不能进行删除,但是可以按模式匹配要求列出目录中所有的文件或子目录,37,随机访问文件,类,RandomAccessFile,来处理这种输入输出,创建一个随机访问文件,使用文件名,使用文件对象,myRAFile=new RandomAccessFile(String name,,,String mode),myRAFile=new RandomAccessFile(File file,,,String mode);,决定是以只读方式(,r,),还是以读写方式,(,rw),访问文件,38,随机访问文件,对象,RandomAccessFile,读写信息的方法同数据输入输出对象的方法相同,它可以访问类,DataInputStream,和,DataOutputStream,中的所有,read(),、,write(),方法,移动文件读写指针的方法:,long getFilePointer(),返回文件指针的当前位置,void seek(long pos),;,将文件指针置于指定的绝对位置。位置值以从文件开始处的字节偏移量,pos,来计算,,pos,为,0,代表文件的开始,long length(),返回文件的长度。位置值为,length(),,,代表文件的结尾,39,Thank You!,40,
    展开阅读全文

    开通VIP折扣优惠下载文档

    提示  咨信网声明:本文档由用户上传并分享,仅供学习交流、研究之用,未经授权,严禁复制、发行、汇编、翻译或网络传播等,侵权必究。

    咨信网小贴示:
    1、浏览或下载可查看了解,请点击【咨信网告知】。
    2、下载文件中如有侵权或不适当内容,将立即纠正。
    3、如果因为网速或其他原因下载失败请重新下载,重复下载[60天内]不扣币。
    4、开具发票登录电脑端申请,请点击【索取发票】。

    咨信网客服组:【微信客服】

    自信AI创作助手
    关于本文
    本文标题:输入输出数据流(用).ppt
    链接地址:https://www.zixin.com.cn/doc/14772274.html

    Copyright ©2010-2026   All Rights Reserved  宁波自信网络信息技术有限公司 版权所有   |  客服电话:0574-28810668    微信客服:咨信网客服    投诉电话:18658249818   

    违法和不良信息举报邮箱:help@zixin.com.cn    文档合作和网站合作邮箱:fuwu@zixin.com.cn    意见反馈和侵权处理邮箱:1219186828@qq.com   | 证照中心

    12321jubao.png12321网络举报中心 电话:010-12321  jubao.png中国互联网举报中心 电话:12377   gongan.png浙公网安备33021202000488号  icp.png浙ICP备2021020529号-1 浙B2-20240490   


    关注我们 :微信公众号  抖音  微博  LOFTER               

    自信网络  |  ZixinNetwork