版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1,第12讲 输入输出数据流,流的概念 流的分类 流的使用(以字节流为例) 标准输入/输出流 文件输入/输出流 过滤流 字符流的使用,2,本讲学习任务之一:,问题:如何编程从键盘输入数据? 任务:编写一个猜数游戏程序,程序生成一个10以内的整数,用户从键盘输入猜测的数,直到猜对为止。两个人轮流猜,每人猜3次,累计猜测次数少的胜。,3,流(Stream)的概念,流是从源到目的地的有序字节序列,具有先进先出的特征。 根据流与程序的关系将流分为输入流和输出流两类。 程序从输入流读取数据;向输出流写出数据。,4,流的概念,源 输入流的源可以是文件、标准输入(键盘)、其他外部输入设备或者其他输入流。 目
2、的地 输出流的目的地可以是文件、标准输出(显示器)、其他外部输出设备或者其他输出流。 Java中输入输出是通过流来实现的。相关的类都在java.io包中。,5,流的分类,输入流/输出流 按流与程序的关系分。如前所述。 字节流/字符流 按流中处理的数据是以字节(8位)为单位还是以字符(16位)为单位分为字节流和字符流。 Java中字节流和字符流分属两个不同的体系。 节点流/过滤流 按流与原始数据载体(文件,设备)的关系分为节点流和过滤流。,6,流的分类(续),节点流(Node Stream) :直接与原始数据存在的特定介质(如磁盘文件或其他外部设备、内存某区域或其他程序)打交道的流,在流的序列中
3、离程序最远。 过滤流 (Filter Stream):使用其它的流作为输入源或输出目的地,对流中的数据提供进一步处理的流。其他的流可以是节点流,也可以是另一种过滤流。过滤流不能单独使用。 一个输入流链或输出流链中一定有且只有一个节点流;可以没有,也可以有多个过滤流。,7,字节流的层次结构,过滤流,结点流,抽象类,8,字符流(读写器)的类层次结构,9,InputStream 类的常用方法,读一个字节,并返回该字节。未读到返回-1 public int read() throws IOException 关闭流 public void close( ) throws IOException 将数据
4、读入字节数组b, 返回所读的字节数 int read(byte b ) throws IOException 将数据读入字节数组b, 返回所读的字节数,offset和length指示byte中存放读入字节的位置。 int read( byte b, int offset, int length ) throws IOException,10,OutputStream的常用方法,写一个字节 void write( int ) throws IOException 关闭输出流 void close( ) throws IOException 强行将缓冲区的数据写到目的地。 void flush(
5、) throws IOException 写一个字节数组 void write(byte b) throws IOException void write(byte b, int offset, int length ) throws IOException,11,标准输入输出流和错误流,System.out: 把输出送到缺省的显示(通常是显示器) System.in 从标准输入获取输入(通常是键盘) System.err 把错误信息送到缺省的显示 System是final类,in,out,err是System的静态成员变量,因此可以用System.in等形式直接使用。out的用法大家已熟知了
6、,err的用法与out一样。,12,标准输入 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时,程序会等待用户的输入。输入完成后再接着执行后面的语句。,13,流的使
7、用过程,输入/输出流的使用过程: 实例化一个输入/输出流对象 使用该输入/输出流对象的方法读入/写出数据 关闭该输入/输出流对象 注意事项 输入/输出流的方法会抛出异常,因此必须进行异常处理。 标准输入/输出/错误流对象System.in, System.out、 System.err始终存在,不需要实例化,也不需要关闭。,14,例:使用System.in实现键盘数据输入,import java.io.*; public class TestInput public static void main(String args) try byte bArray=new byte128; Syste
8、m.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(); ,15,本讲学习任务之二:,学会如何对文件进行读写操作 任务:编写一个程序,统计一个磁盘文本文件中的单词数。,16,从文件读数据:FileInputStream,InputStream是抽象类,不能直接用new Inpu
9、tStream()构造实例。 FileInputStream是InputStream的子类,可以生成实例。 FileInputStream有三个构造方法,最常用的构造方法如下: Public FileInputStream(String fileName) throws FileNotFoundException Public FileInputStream(File file) throws FileNotFoundException fileName用来指定输入文件名及其路径,file是一个File对象,17,例:读出一个文本文件的内容并显示,import java.io.*; class
10、 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.
11、txt不存在!); catch(IOException e)System.out.println(发生输入输出错误!); ,18,向文件写数据:FileOutputStream,OutputStream是抽象类,不能直接用new OutputStream()构造实例。 FileOutputStream是OutputStream的子类,可以生成实例。 FileOutputStream有5个构造方法,最常用的构造方法如下: Public FileOutputStream(String name) throws FileNotFoundException 和 Public FileOutputStr
12、eam(String name,boolean append) throws FileNotFoundException Name用来指定输入文件名及其路径,append为true时数据将添加到文件已有内容的末尾。,19,例题:使用FileOutputStream实现文件复制。,import java.io.*; class CopyAFile public static void main(String args) InputStream in; OutputStream out; try in=new FileInputStream(test.txt); out=new FileOutpu
13、tStream(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.t
14、xt不存在!); catch(IOException e) System.out.println(发生输入输出错误!); ,20,本讲学习任务之三:,学会使用过滤流(缓冲流,数据流等) 任务: 使用缓冲流(BufferedInputStream和BufferedOutputStream)改写前面的文件读写程序。 编写一个程序,将10个整型数据写入文件,再读出来求和。,21,BufferedInputStream,是过滤流。 数据流从原始流成块读入数据,放在一个内部字节数组中。通过减少IO次数提高效率。 构造方法 BufferedInputStream(InputStreamin) Buffer
15、edInputStream(InputStreamin, intbuffersize) 基本方法: int read() throws IOException int read(byte, int offset, int length) throws IOException void close() throws IOException,22,BufferedOutputStream,是过滤流。 将数据积累到一个大数据块后再成批输出。通过减少IO次数提高效率。 构造方法: BufferedOutputStream(OutputStreamout) BufferedOutputStream(Ou
16、tputStreamout, intbuffersize) 基本方法: void write(int c) throws IOException void write(byte , int offset, int length ) throws IOException void flush() throws IOException void close() throws IOException,23,输入/输出流的套接,通过流的套接,可以利用各种流的特性共同处理数据。 如:,数据源,程序,数据源,程序,注意:直接对数据源读写的是节点流,不能是过滤流。过滤流通常必须套接节点流。,24,流的套接举
17、例: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
18、(); in.close(); ,25, 属过滤流。能按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
19、(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,26,过滤流示例,/使用DataOutputStream将一些数据写入文
20、件,再用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 un
21、its = 12, 8, 13, 29, 50 ; String descs = Java T-shirt, Java Mug, Duke Juggling Dolls, Java Pin, Java Key Chain ;,27,过滤流示例,过滤流示例,/使用DataOutputStream将一些数据写入文件,再用DataInputStream读入 /DataIOTeat.java import java.io.*; public class DataIOTest public static void main(String args) throws IOException / 注意也套接了
22、节点流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 ;,28,for (int i = 0; i prices.leng
23、th; 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;,29,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 + a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026广东佛山市华材职业技术学校招聘合同人员2人考试备考题库及答案解析
- 2026年元宇宙行业未来五年技术演进与生态构建预测分析报告
- 2026年医院限制类医疗技术规范化管理实施方案
- 格构柱专项施工方案
- 2026年蚌埠市住房和城乡建设局选聘专职人民调解员笔试模拟试题及答案解析
- 2026年危化品泄漏应急处理能力考核试卷及答案
- 污水处理站基础施工方案
- 2026广东江门市某机关事业单位行政综合专员招聘1人笔试参考题库及答案解析
- 2026重庆市大足区国衡商贸有限责任公司招聘劳务派遣制工作人员(公车驾驶员)1人考试参考题库及答案解析
- 2026东北师范大学东北民族民俗博物馆招聘2人备考题库及答案详解(新)
- 公路工程高精度GNSS测量技术规范
- 幼儿园谷雨课件
- 量子计算入门:通过线性代数学习量子计算 课件 第11章 量子傅里叶变换
- 行政处罚法专题培训课件
- 统计知识党校培训课件
- 2025年四川省泸州市中考道德与法治真题(附答案解析)
- 传统曲艺进高校活动方案
- 心电图基础知识与识图理论考核试题题库及答案
- 2025年四川省德阳市中考一模化学试题(含答案)
- 智能化弱电培训
- 杭州中好电子有限公司搬迁项目环评报告
评论
0/150
提交评论