java 输入输出ppt_Java第八章输入输出流.ppt

第8章输入输出数据流 南京农业大学谢忠红 输入输出数据流 1 数据流的基本概念 2 java io包中的输入输出流的基类 3 基本输入输出流的子类 文件流 缓冲输入输出流 4 IOException异常类的子类 5 随机存取文件 6 输入输出流的高级应用管道流和数据流 1 数据流的基本概念 什么是数据流 文件 终端 文件 网络端点 数据流 起点 终点 网络端点 程序的内存区 键盘 数据流是指所有的数据通信通道包括输入流和输出流 java lang SystempublicfinalclassSystemextendsObject staticPrintStreamerr 标准错误流 输出 staticInputStreamin 标准输入 键盘输入流 staticPrintStreamout 标准输出流 显示器输出流 Java的标准的数据流 注意 1 System类不能创建对象 只能直接使用它的三个静态成员 2 每当main方法被执行时 就自动生成上述三个对象 System err把错误信息送到缺省的显示 System out 把输出送到缺省的显示 通常是显示器 Voidprint 参数 Voidprintln 参数 System in从标准输入获取输入 通常是键盘 intread 返回ASCII码 若 返回值 1 说明没有读取到任何字节读取工作结束 intread byte b 读入多个字节到缓冲区b中返回值是读入的字节数 importjava io classTestKeyRead publicstaticvoidmain Stringargs intb try while b System in read 1 System out print char b catch IOExceptione System out println e toString 标准输入流System in read byte buffer newbyte 512 System out println pleaseInput intcount System in read buffer System out println Output for inti 0 i count i System out print buffer i System out println for inti 0 i count i System out print char buffer i Strings newString buffer System out println s System out println count 标准输入流System in read byte b 程序输出 2 java io包中的输入输出流的基类 输出流 将程序中的数据输出到外界 显示器 打印机 文件 网络 的通信通道 输出流的基本类 OutputStream 输入流 将数据从外界 键盘 文件 网络 读入到程序的通信通道 输入流的基类 InputStream InputStream及其子类 InputStream FileInputStream PipedIntputStream FilterInputStream ByteArrayInputStream DataInputStream BufferedInputStream LineNumberInputStream SequencedInputStream StringBufferInputStream PushbackInputStream 抽象类InputStream InputStream类的常见方法 intread 读一个byte类型数 返回读入字节 若返回值 1 说明没有读取到任何字节读取工作结束intread byteb 读多个字节到数组b中intread byteb intoff intlen 从输入流中读取len个字节放入数组b中 起始位置offavailable 返回输入流中可以读取的字节数close 关闭流 OutputStream及其子类 OutputStream FileOutputStream PipeOutputStream FilterOutputStream ByteArrayOutputStream DataOutputStream BufferedOutputStream PrintStream 抽象类OutputStream OutputStream类的常见方法 voidwrite intb 将一个整数输出到流中voidwrite byteb 将数组中的数据输出到流中 voidwrite byteb intoff intlen 将数组b中从off指定的位置开始len长度的数据输出到流中close 关闭流 注意 上述各方法都有可能引起异常 3 基本输入输出流的子类 文件流 file1 txt file2 txt FileInputStream FileOutputStream 通信通道 内存 举例 利用程序 将文件file1 txt拷贝到file2 txt中 注意 要捕获文件异常 内存 涉及到三个问题 1 怎样表示文件 文件类 2 怎样实现文件的输入 文件输入流 3 怎样实现文件的输出 文件输出流 文件类 File类 io包 作用 File类主要用于命名文件 查询文件属性和处理文件目录 publicclassFileextendsObjectimplementsSerializable Comparable 构造函数File Stringpathname File Stringparent Stringchild File Fileparent Stringchild 例 Filef1 newFile FileTest1 txt 创建文件对象f1 f1所指的文件是在当前目录下创建的FileTest1 txt例 Filef2 newFile D dir1 FileTest2 txt 注意 D dir1目录事先必须存在 否则异常例 Filef4 newFile dir3 Filef5 newFile f4 FileTest5 txt 在如果 dir3目录不存在使用f4 mkdir 先创建 publicclassFileTestExp publicstaticvoidmain Stringargs throwsIOException Filedir newFile root Filef1 newFile dir fileOne txt Filef2 newFile dir fileTwo java 文件对象创建后 指定的文件或目录不一定物理上存在if dir exists dir mkdir if f1 exists f1 createNewFile if f2 exists f2 createNewFile 说明 File类的方法 1 exists 测试磁盘中指定的文件或目录是否存在 2 mkdir 创建文件对象指定的目录 单层目录 3 createNewFile 创建文件对象指定的文件 System out println f1 sAbsolutePath f1 getAbsolutePath System out println f1Canread f1 canRead System out println f1 slen f1 length String FL intcount 0 FL dir list for inti 0 i FL length i count System out println FL i isin root System out println thereare count filein root 说明 File类的方法 4 list 返回目录中所有文件名字符串 返回 文件输入流 FileInputStream类 作用 以文件作为数据输入源的数据流 或者说是打开文件 从文件读数据到内存的类 使用方法 1 Filefin newFile d abc txt FileInputStreamin newFileInputStream fin 使用方法 2 FileInputStreamin newFileInputStream d abc txt 输入源的文件名 程序举例 将InputFromFile java的程序的内容显示在显示器上 显示器 标准输出流 try FileInputStreamrf newFileInputStream InputFromFile java intn 512 bytebuffer newbyte n while rf read buffer 0 n 1 返回 文件输出流 FileOutputStream类 d myjava write txt FileOutputStream 内存 FileoutputStream类作用 用来处理以文件作为数据输出目的数据流 或者说是从内存区读数据入文件 使用方法 1 Filef newFile d myjava write txt FileOutputStreamout newFileOutputStream f 使用方法 2 FileOutputStreamout newFileOutputStream d myjava write txt 数据写入的文件名 程序举例 使用键盘输入一段文章 将文章保存在文件write txt中 Write txt FileoutputStream try System out println pleaseInputfromKeyboard intcount n 512 bytebuffer newbyte n count System in read buffer FileOutputStreamwf newFileOutputStream d myjava write txt wf write buffer 0 count wf close 当流写操作结束时 调用close方法关闭流 System out println Savetothewrite txt catch IOExceptionIOe System out println FileWriteError FileInputStream流和FileOutputStream的应用 例 利用程序将文件file1 txt拷贝到file2 txt中 注意 要捕获文件异常 内存 程序设计 创建一个copy java文件 并在该文件中实现将copy java的内容拷贝到copy2 java中 classcopy publicstaticvoidmain Stringargs try FileinFile newFile copy java FileoutFile newFile copy2 java FileInputStreamfinS newFileInputStream inFile FileOutputStreamfoutS newFileOutputStream outFile intc while c finS read 1 foutS write c finS close foutS close catch IOExceptione System err println FileStreamsTest e 4 基本输入输出流子类之二 缓冲输入输出流 文件 输入流 缓冲输入流 键盘 文件 显示器 输出流 缓冲输出流 目的 增加缓冲区流 减少访问硬盘的次数 提高效率 BufferedInputStream和BufferedOutputStream 内存 将BufferedInputStream与FileInputStream相接FileInputStreamin newFileInputStream file1 txt BufferedInputStreambin newBufferedInputStream in 文件 文件输入流 缓冲输入流 内存 内存 将BufferedOutputStream与FileOutputStream相接FileOutputStreamout newFileOutputStream file1 txt BufferedOutputStreambin newBufferedInputStream out 将BufferedReader与标准的数据流相接InputStreamReadersin newInputStreamReader System in BufferedReaderbin newBufferedReader sin 键盘 键盘输入流 缓冲输入流 内存 publicclassReadWriteToFile publicstaticvoidmain Stringargs throwsIOException InputStreamReadersin new InputStreamReader System in BufferedReaderbin newBufferedReader sin FileWriterout newFileWriter myfile txt BufferedWriterbout newBufferedWriter out Strings while s bin readLine length 0 bout write s 0 s length bin close bout close 程序说明 从键盘读入字符 并写入到文件中BufferedReader类的方法 StringreadLine 作用 读一行字符串 以回车符为结束 BufferedWriter类的方法 bout write Strings offset len 作用 从缓冲区将字符串s从offset开始 len长度的字符串写到某处 InputStream和OutputStream类处理的是字节流 数据流中的最小单位是字节 8个bit Reader与Writer处理的是字符流 在处理字符流时涉及了字符编码的转换问题 Unicode编码 java GBK编码UTF 8编码 publicclassEncodeTest privatestaticvoidreadBuff byte buff throwsIOException ByteArrayInputStreamin newByteArrayInputStream buff intdata while data in read 1 System out print data System out println in close publicstaticvoidmain Stringargs throwsIOException System out println 内存中采用unicode字符编码 charc 好 intlowBit c 内存中采用unicode字符编码 12589本地操作系统默认字符编码 186195采用GBK字符编码 186195采用UTF 8字符编码 229165189 内存 使用Unicode字符编码的字符串 数据源 数据汇 使用本地操作系统的字符编码的字符串 Reader的read 方法 Writer的write 方法 Reader类能够将输入流中采用其他编码类型的字符转换为Unicode字符 然后在内存中为其分配内存Writer类能够将内存中的Unicode字符转换为其他编码类型的字符 再写到输出流中 importjava io publicclasstest publicstaticvoidmain Stringargs throwsIOException StringReaderreader newStringReader 好 FileOutputStreamfoutS newFileOutputStream a1 txt BufferedOutputStreambout newBufferedOutputStream foutS FileWriterFout newFileWriter a2 txt intdata while data reader read 1 bout write char data Fout write char data reader close bout close Fout close 4 IOException异常类的子类 1 publicclassEOFException 非正常到达文件尾或输入流尾时 抛出这种类型的异常 2 publicclassFileNotFoundException 当文件找不到时 抛出的异常 3 publicclassInterruptedIOException 当I O操作被中断时 抛出这种类型的异常 5 文件的随机读写 随机读写文件类 RandomAcessFile类作用 实现文件的随机读写 即从文件的任意位置开始读或写 classRandomFileextendsObjectimplementsDataOutput DataInput 构造函数 RandomAccessFile StringFilename Stringmode mode r 只读方式Mode rw 读写方式常见的方法 seek longpos 作用 将文件指针移动至文件pos位置longgetFilePointer 作用 返回当前的文件指针的位置longlength 作用 返回文件的长度 intread 作用 从文件中读一个byteStringreadLine 作用 从文件中读下一行StringreadUTF 作用 从文件中读一个字符串DoublereadDouble 作用 从文件中读一个Double型数 VoidwriteUTF 作用 向文件中写一个字符串 字符集是UTF 8 VoidwriteDouble 作用 向文件中写一个Double型数VoidwriteByte 作用 向文件中写一个byte型的数VoidwriteBytes Strings 作用 向文件中写一字符串 在文件中一字节串形式保存 程序示例1 向文件的末尾添加一个字符串classtestAccessFile publicstaticvoidmain Stringargs try RandomAccessFilerf1 newRandomAccessFile testAccess txt rw rf1 writeBytes Linda rf1 writeBytes 21 rf1 writeBytes female rf1 writeBytes ComputerDepart rf1 seek rf1 length rf1 writeUTF helloeveryone catch Exceptione System out println e toString 程序示例 将文件中的 xiufang 改为 liufang classFileAccess publicstaticvoidmain Stringargs try RandomAccessFilerf1 newRandomAccessFile testAccess txt rw rf1 writeLong 11231 rf1 writeUTF xiufang rf1 writeInt 21 rf1 writeChar f rf1 writeUTF ComputerDepartment rf1 writeDouble 561 5 rf1 seek 10 rf1 writeBytes liu catch Exceptione System out println e toString 6 对象流 定义 能够输入输出对象的流 将对象在文件之间或网上进行传递 文件 网络 对象流 内存 文件 网络 对象流 ObjectInputStreamObjectOutputStream 文件输入流 文件输出流 对象序列化和对象流 1 为什么序列化 对象不能够直接进行输入输出 2 什么是序列化 对象通过写出描述自己状态的的数值来记录自己的过程 3 两者什么关系 将序列化后的对象通过对象输入输出流写入文件或传送到网络其他位置 1 只有实现serializable接口的类才能被串行化classStudentimplementsSerializable intid Stringname intage Stringdepartment publicStudent intid Stringname intage Stringdepartment this id id this name name this age age this department department 对象流举例 将Student类的一个实例写到文件中importjava io publicclassObjectser publicstaticvoidmain Stringargs Studentstu newStudent 981036 LiMing 16 CSD try FileOutputStreamfo newFileOutputStream date ser ObjectOutputStreamso newObjectOutputStream fo so writeObject stu so close catch Exceptione 对象流 从文件date ser中读一个Student类的一个实例出来 Studentstu null try FileInputStreamfi newFileInputStream date ser ObjectInputStreamsi newObjectInputStream fi stu Student si readObject si close catch Exceptione System out println e toString System out println ID stu id name stu name age stu age dept stu department

展开阅读全文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值