版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、文件与IO流,2,主要内容,流的概念 流的分类 流的使用(以字节流为例) 标准输入/输出流 文件输入/输出流 字符流的使用,3,流(Stream)的概念,流是从源到目的地的有序字节序列,具有先进先出的特征。 根据流与程序的关系将流分为输入流和输出流两类。 程序从输入流读取数据;向输出流写出数据。,4,流的概念,源 输入流的源可以是文件、标准输入(键盘)、其他外部输入设备或者其他输入流。 目的地 输出流的目的地可以是文件、标准输出(显示器)、其他外部输出设备或者其他输出流。 Java中输入输出是通过流来实现的。相关的类都在java.io包中。,5,流的分类,输入流/输出流 按流与程序的关系分。
2、字节流/字符流 按流中处理的数据是以字节(8位)为单位还是以字符(16位)为单位分为字节流和字符流。 Java中字节流和字符流分属两个不同的体系。,6,字节流的层次结构,过滤流,结点流,抽象类,7,字符流的类层次结构,8,InputStream类的常用方法,读一个字节,并返回该字节。未读到返回-1 public int read() throws IOException 关闭流 public void close( ) throws IOException 将数据读入字节数组b, 返回所读的字节数 int read(byte b ) throws IOException 将数据读入字节数组b,
3、 返回所读的字节数,offset和length指示byte中存放读入字节的位置。 int read( byte b, int offset, int length ) throws IOException,9,OutputStream的常用方法,写一个字节 void write(int) throws IOException 关闭输出流 void close( ) throws IOException 将缓冲区的数据写到目的地。 void flush( ) throws IOException 写一个字节数组 void write(byte b) throws IOException void
4、 write(byte b, int offset, int length ) throws IOException,10,标准输入输出流,System.out: 把输出送到缺省的显示(通常是显示器) System.in 从标准输入获取输入(通常是键盘) System是final类,in,out是System的静态成员变量,因此可以用System.in等形式直接使用。,11,标准输入 System.in,在System中,in的完整定义是: public static final InputStream in; in的主要方法: public int read() throws IOExcep
5、tion public int read(byte b) throws IOException 使用注意事项: 前者返回读入的一字节的数据,但返回的是int整型值,取值范围是0-255。 后者返回读入的字节数,读入的各字节保存在作为参数的字节型数组对象中。 执行read时,程序会等待用户的输入。输入完成后再接着执行后面的语句。,12,流的使用过程,输入/输出流的使用过程: 实例化一个输入/输出流对象 使用该输入/输出流对象的方法读入/写出数据 关闭该输入/输出流对象 注意事项 输入/输出流的方法会抛出异常,因此必须进行异常处理。 标准输入/输出流对象System.in, System.out始
6、终存在,不需要实例化,也不需要关闭。,13,例:使用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(IOExce
7、ption ioe) System.out.println(ioe.toString(); ,14,从文件读数据:FileInputStream,FileInputStream是InputStream的子类,可以生成实例。 FileInputStream有三个构造方法,最常用的构造方法如下: Public FileInputStream(String fileName) throws FileNotFoundException Public FileInputStream(File file) throws FileNotFoundException fileName用来指定输入文件名及其路径
8、,file是一个File对象,15,例:读出一个文本文件的内容并显示,import java.io.*; class ReadFromFile public static void main(String args) InputStream 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)Sy
9、stem.out.println(当前目录下文件test.txt不存在!); catch(IOException e)System.out.println(发生输入输出错误!); ,16,向文件写数据:FileOutputStream,FileOutputStream是OutputStream的子类,可以生成实例。 FileOutputStream有5个构造方法,最常用的构造方法如下: Public FileOutputStream(String name) throws FileNotFoundException 和 Public FileOutputStream(String name,b
10、oolean 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(copyResul
11、t.txt); 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,
12、BufferedInputStream,数据流从原始流成块读入数据,放在一个内部字节数组中。通过减少IO次数提高效率。 构造方法 BufferedInputStream(InputStreamin) BufferedInputStream(InputStreamin, intbuffersize) 基本方法: int read() throws IOException int read(byte, int offset, int length) throws IOException void close() throws IOException,19,BufferedOutputStream,
13、将数据积累到一个大数据块后再成批输出。通过减少IO次数提高效率。 构造方法: BufferedOutputStream(OutputStreamout) BufferedOutputStream(OutputStreamout, intbuffersize) 基本方法: void write(int c) throws IOException void write(byte , int offset, int length ) throws IOException void close() throws IOException,20,例子:缓冲数据输入流,import java.io.*; c
14、lass 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(); ,21, 按java的基本数据类型读写流中的数据: DataInputStream方
15、法 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 writeD
16、ouble(double)void writeFloat( float) void writeshort(short)void writeInt ( int) void writeBytes(String)void writeChars(String ) void WriteUTF(String str)/将字符串以UTF格式写出,数据输入输出流,22,例子:数据输入输出流,/使用DataOutputStream将一些数据写入文件,再用DataInputStream读入 /DataIOTeat.java import java.io.*; public class DataIOTest pub
17、lic static void main(String args) throws IOException DataOutputStream out = new DataOutputStream(new FileOutputStream(invoice.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 ;,
18、23,for (int i = 0; i prices.length; i +) out.writeDouble(pricesi); out.writeChar(t); out.writeInt(unitsi); out.writeChar(t); out.writeUTF(descsi); out.writeChar(n); out.close(); / read it in again DataInputStream in = new DataInputStream(new FileInputStream(invoice.txt); double price; int unit; Stri
19、ng desc; double total = 0.0;,24,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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广东省广州番禺区七校联考2025-2026学年初三下学期周测英语试题含解析
- 2026届重庆市北碚区初三下学期联考押题卷语文试题试卷含解析
- 信息系统安全稳定维护策略
- 能源管控成果提升承诺书3篇范文
- 教育科学研究公平保证函9篇
- 科研成果创新维护承诺书6篇范文
- 智慧城市建设贡献承诺书6篇
- 企业培训需求分析模板员工成长与企业发展双赢版
- 智能出行技术指南手册
- 供应商交货周期协商商洽函8篇
- 2026管理综合面试题及答案
- 2026年安徽扬子职业技术学院单招职业技能考试题库附答案详解(预热题)
- 2025年河南经贸职业学院单招职业技能考试试题及答案解析
- 2026年南通师范高等专科学校单招职业适应性考试题库附参考答案详解(考试直接用)
- 2026森岳科技(贵州)有限公司招聘工作人员29人考试备考试题及答案解析
- 2026及未来5年中国钢板桩行业市场行情动态及发展前景研判报告
- 2025年北京市第二次普通高中学业水平合格性考试地理仿真模拟地理试卷01(解析版)
- 住院病历书写规范2026
- 2026年浙江省十校联盟高三3月质量检测试题试英语试题试卷含解析
- 封装热管理模型优化多芯片散热效率
- 雨课堂学堂在线学堂云《导弹总体设计导论(国防科技)》单元测试考核答案
评论
0/150
提交评论