java 输入输出ppt_Java的输入与输出.ppt

本文详细介绍了Java中的文件输入输出操作,包括字节文件的创建、读取和拷贝,以及数据文件的建立与读取。通过示例代码展示了如何使用FileOutputStream、FileInputStream、DataStream和RandomAccessFile类进行文件操作,同时讲解了基本类型数据的输入输出。文章还提及了文件拷贝和随机访问文件的操作,以及如何利用DataOutputStream和DataStream读写数据文件。
摘要由CSDN通过智能技术生成

11cbc1e44d50bc91599981632979f685.gifJava的输入与输出.ppt

Java的输入与输出 本章主要讲述了基本数据流类 文件处理 例8 1建立字节文件importjava io classCreateFile publicstaticvoidmain String args throwsIOException inti 0 len ch byte b newbyte 20 FileOutputStreamout newFileOutputStream ex0801 out while ch System in read 1 b i byte ch i out write b 0 i out close 创建文件输出流out 文件ex0801 out的指针指向文件的第1个字节 从键盘输入不多于 个的字符并将它们存入字节数组b 将b中从下标 开始的i字节写入out 关闭out publicFileOutputStream Stringpath throwsFileNotFoundException用于创建名为path的文件并创建文件输出流对象 该输出流对象以名为path的文件为数据终点 若所指定的文件不能打开 则抛出FileNotFoundException异常 当前安全管理不允许创建或修改指定文件 则抛出SecurityException异常 在字符串path中可包含文件路径 如 myjava ch8 ex0801 out 例8 2读字节文件importjava io classReadByteFlie publicstaticvoidmain String args charch inti len byte b newbyte 20 try FileStreamin newFileStream ex0801 out System out println 可读的字节数 len in available in skip 3 System out println 读取的字节数 in read b 0 len 4 in close System out print 所读字节值 for i 0 i len 4 i System out print b i System out println System out print 所读字节对应的字符 for i 0 i len 4 i System out print char b i System out println catch IOExceptione System err println e toString 若文件ex0801 out是例8 1程序所建立的 本程序运行后产生的输出是 可读的字节数 12读取的字节数 8所读字节值 100101102103104105106107所读字节对应的字符 deghijk FileStream类称为文件输入流类 用于读取文件中的字节数据 该类是Steam类的子类 它实现了Steam类中的抽象方法read publicFileStream Stringname throwsFileNotFoundExcepion用于打开一个名为name的文件 包含路径 并创建文件输入流对象 该输入流对象以名为的文件为数据源 若所指定的文件不能打开则抛出FileNotFoundException异常 若当前安全管理器不允许读取指定文件 则抛出SecurityException异常 例8 3文件拷贝importjava io classCopyFile publicstaticvoidmain String args if args length 2 System out println 程序的用法 javaCopyFile源文件名目标文件名 System exit 1 try FileStreamin newFileStream args 0 FileOutputStreamout newFileOutputStream args 1 intch while ch in read 1 out write ch in close out close catch IOExceptione System err println e toString 该程序以原文件名和目标文件名为参数 其中源文件是已存在的文件 FileStream和FileOutputStream只支持对字节数据的输入和输出 是一种字节文件流类 例8 4建立数据文件importjava io classCreateDataFile publicstaticvoidmain String args booleanlogic true shorts 32768 inti 65534 longl 131071 floatf float 1 4142 doubled 3 14159265359 Stringstr1 Hello Stringstr2 欢迎来到JAVA乐园 Stringstr3 祝你成功 try DataOutputStreamout newDataOutputStream newFileOutputStream ex0804 out out writeBoolean logic out writeByte i out writeShort s out writeInt i out writeLong l out writeFloat f out writeDouble d out writeBytes str1 out writeChars str2 out writeUTF str3 out close catch IOExceptione System err println e toString 基本类型数据输入输出接口Data与DataOutput 基本类型数据输入输出流类DataStream与DataOutputStream DataoutputStream类具备将基本类型数据写入输出流的能力 该类的构造方法是 publicDataoutputStream OutputStreamout 例8 5读数据文件importjava io classReadDataFile publicstaticvoidmain String args try DataStreamin newDataStream newFileStream ex0804 out System out println logic in readBoolean System out println b in readByte System out println s in readShort System out println i in readInt System out println l in readLong System out println f in readFloat System out println d in readDouble byte b newbyte 5 in readFully b System out print strl for intk 0 k 5 k System out print char b k System out println StringBufferstr2 newStringBuffer for intk 0 k 11 k str2 append in readChar System out println str2 str2 toString System out println str3 in readUTF in close catch IOExceptione System err println e toString 若文件ex0848 out是由例8 4的程序产生的 本程序产生的输出为 logic trueb 2s 32768i 65534l 131071f 1 4142d 3 14159265359str1 Hellostr2 欢迎来到Java乐园 str3 祝你成功 本程序中各个读语句的次序不能改变 这个次序与例8 4中各个写语句写入数据的次序相对应 DataStream类实现了接口Data中的所有方法 适用于读取由DataOutputStream类的方法所建立的数据文件 该类是FilterStream类的子类 要求用其他输入流对象来构造本类的对象 该类的构造方法是 publicDataStream Streamin 例8 7使用随机访问文件importjava io classRandomFile publicstaticvoidmain String args int intArray 18 5 12 27 10 36 8 54 try RandomAccessFileraf newRandomAccessFile ex0808 dat rw for inti 0 i intArray length i raf writeInt intArray i for inti intArray length 1 i 0 i 2 raf seek i 4 System out print raf readInt System out println raf seek 4 raf writeInt 100 raf seek 0 do System out print raf readInt while raf getFilePointer raf length raf close System out println catch IOExceptione System out print e toString 该程序产生的输出是 54362751810012271036854 RandomAccessFile类不是Stream类的子类也不是OutStream类的子类而是Object类的子类 该类对象既具有读文件的能力又有写文件的能力 该类对象还具备对文件进行随机读写的能力而不是顺序读写 该类构造方法为 publicRandomAccessFile stringpath stringmode 参数path是要对其读写的文件的文件名 包含路径 参数mode为文件存取模式 若mode值为 则表示文件只可读 若mode为 则表示文件既可读又可写 例8 7记录的输入与输出importjava io publicclassOneRecord intnumber Stringname charsex doubleaveIncome publicOneRecord publicOneRecord intnumber Stringname charsex doubleaveIncome this number number this name name this sex sex this aveIncome aveIncome publicvoidwrite RandomAccessFilerf throwsIOException rf writeInt number rf writeBytes name rf writeChar sex rf writeDouble aveIncome publicvoidread RandomAccessFilerf throwsIOException number rf readInt byte b newbyte 10 rf readFully b name newString b 0 sex rf readChar aveIncome rf readDouble publicintsize return24 importjava io classTestOneRecord publicstaticvoidmain String args throwsIOException RandomAccessFilef newRandomAccessFile workerRecord dat rw DataStreamin newDataStream newFileStream workers dat for inti 0 i 2 i Strings in readLine intnumber Integer valueOf s intValue Stringname in readLine charsex in readLine charAt 0 s in readLine charsex in readLine charAt 0 s in readLine doubleaveIncome Double valueOf s doublue OneRecordone newOneRecord number name sex aveIncome one write f OneRecordone newOneRecord System out println 职工编号 姓名 性别 平均收入 intrecordNumber int f length one size f seek 0 for inti 0 i recordNumber 1 i one read f System out println one number one name one sex one aveIncome f close 若workers dat的内容是 101aaaaaaaaaaf999 9102bbbbbbbbbbm888 8103ccccccccccf777 7 该程序的输出是 职工编码姓名性别平均收入101aaaaaaaaaaf999 9102bbbbbbbbbm888 8103ccccccccccf777 7 由于DataStream类中的方法readLine不支持Unicod字符 因此在文件worker dat的姓名和性别不能用中文表示 在上述测试程序中第5行中的 newFileStream workers dat 可用System in代替 但在运行程序时应当从键盘输入workers dat中的内容 容 一个记录由多个域组成 对于1个域而言 可以是基本类型或字符串或数组 将一个记录写入文件的方法就是将记录的各个域依次写入以文件为终点的输出流中 从文件中读出一个记录的方法是在以记录为单位的文件位数据源点的输入流中依次读出记录的各个域 为了使得以记录为单位对文件进行读写更为方便 可以为记录设计相应的类 在类的成员中包括记录的各个域和将记录写入流和从流中读出记录的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值