第20章-数据流1.ppt_第1页
第20章-数据流1.ppt_第2页
第20章-数据流1.ppt_第3页
第20章-数据流1.ppt_第4页
第20章-数据流1.ppt_第5页
免费预览已结束,剩余26页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

文件 程序 终端 文件 程序 网络端点 数据流 起点 终点 网络端点 文件 字符串存储区 一 输入输出方法 什么是数据流 流是可被顺序访问的无限长的字符序列 在java中有关流的操作使用io包importjava io 流的分类 流 字节流 字符流 Java io中的四个类 以字节为对象 输入流 InputStream输出流 OutputStrea以字符为对象 输入流 Reader输出流 Writer 字节流和字符流的比较 字节流 适用于各类文件每次读写8位字节效率较低 字符流适用于16位的字符文件每次读写16位字符效率较高 8 2文件处理 1 File类2 字符文件 FileReader类 FileWriter类3 字节文件 FileInput类 FileOutput类 定义在包java io中 1 File类 1 所有对文件的操作都要使用File类 2 其构造 File 文件名 File 目录名 文件名 文件与应用程序不在同一目录 2 操作字符文件的类 FileReader读取文件 FileWriter写入文件 BufferedReader输入到缓冲区 BufferedWriter输出到缓冲区 字符流的读写操作方法 从输入流中按行读取字符的方法 StringreadLine 向输出流写入多个字符的方法 write Strings intoff intlen 将指定的字符串s从偏移量off开始的len个字符写入文件输出流 while s bf readLine null text append s n Filef newFile d jtest e txt fr newFileReader f bf newBufferedReader fr 用循环按行读取 3 读取文本文件 建立输入通道 完成读取动作 例1 设计一个读取文本文件的应用程序 1 建立一个窗体程序 2 内置一个文本区 3 用字符输入流读取文本文件 4 在文本区中显示读取到的数据 例1的核心语句 try Filef newFile e txt FileReaderfr newFileReader f BufferedReaderbuffin newBufferedReader fr while s buffin readLine null txt append s n catch IOExceptione 4 保存文本文件 fw newFileWriter b txt bw newBufferedWriter fw 建立输出通道 Stringstr txt getText bw write str 0 str length bw flush 完成写的动作 刷新缓冲区强制写入 例2 在一个文本区中写入字符内容 保存为文件 1 建立一个窗体程序 2 内置一个文本区 3 用字符输入流读取文本文件 4 在文本区中显示读取到的数据 例2的核心语句 try w file newFileWriter b txt buf writer newBufferedWriter w file Stringstr txt getText buf writer write str 0 str length buf writer flush catch IOExceptionew System out println ew 一 文本文件的处理 1 File类2 FileReader类3 FileWriter类4 BufferedReader类 1 File类 1 所有对文件的操作都要使用File类 2 其构造 File 文件名 File 目录名 文件名 2 读取文本文件 while s buffin readLine null text append s n Filef newFile d jtest e txt fin newFileReader f buffin newBufferedReader fr 按行读取 例 读取文件 构造有读取文件功能的窗体类 classEWindowextendsFrameimplementsActionListener TextAreatext Buttonbutton BufferedReaderbuffin FileReaderfin EWindow 构造函数 super 流的读取 setSize 200 200 setVisible true addWindowListener newWindowAdapter publicvoidwindowClosing WindowEvente setVisible false System exit 0 构造函数结束 text newTextArea 10 10 button newButton 读取 button addActionListener this setLayout newBorderLayout add text Center add South button try while s buffin readLine null text append s n catch IOExceptionexp publicvoidactionPerformed ActionEvente Strings try Filef newFile e dd E2 html fin newFileReader f buffin newBufferedReader fin catch IOExceptione 窗体类结束 主程序 publicclassE publicstaticvoidmain Stringargs EWindoww newEWindow w pack 3 把文本框中内容写入文件 try fout newFileWriter hello txt buffout newBufferedWriter fout Stringstr text getText text为文本框buffout write str 0 str length buffout flush catch IOExceptionexp System out println haveproblem 刷新缓冲区强制写入 二 二进制文件的处理 文件对象的建立Filefp newFile file1 dat FileInputStream类用来打开一个输入文件FileOutputStream类用来打开一个输出文件 二 二进制文件的处理 FileInputStream类的常用方法 read 从流中读入数据close 关闭流FileOutputStream类的方法 write byteb intoff intlen 在数组b中 从off开始 写入len个字节的数据 二 二进制文件的处理 Filefile newFile d jtest test dat fileInput newFileInputStream file 注意 二进制文件必须按字节读取 见P236例8 1 bytebuffer newbyte 2056 intbytes fileInput read buffer 0 2056 str newString buffer 0 bytes 建立输入通道 完成读的动作 例 将一个文件内容复制到另一个文件中 try FileinFile newFile file1 dat FileoutFile newFile file2 dat fis newFileInputStream inFile fos newFileOutputStream outFile intc while c fis read 1 fos write c fis close fos close catch FileNotFoundExceptione catch IOExceptionee 三 随机存取文件 类RandomAccessFilezip文件需要用随机方法处理文件目录给出个文件的入口 可以随机读取 创建一个随机文件newRandomAccessFile file1 txt r newRandomAccessFile file2 txt rw 随机文件可以同时完成读和写操作 三 随机存取文件 支持随机文件操作的方法 readXXX 或writeXXX 见教材P243表8 3 skipBytes 将指针向下移动若干字节seek 将指针调到所需位置getFilePointer 返回指针当前位置length 返回文件长度利用seek longpos 方法查找随机文件中的信息例 显示读取文件A dat 见下页 五 随机存取文件 importjava io classr publicstaticvoidmain Stringargs try RandomAccessFilefile newRandomAccessFile A dat rw longfilePoint 0 定义指针longfileLength file length while filePoint fileLength Strings file readLine 读取文件中的数据System out println s filePoint file getFilePointer file close catch Exceptione 随机流 获取文件长度 移动记录指针 四 在Java中调用外部程序 运行可执行文件的Runtime类try

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论