当前位置: 网学 > 编程文档 > JAVA > 正文

彻底明白 Java 语言中的IO系统

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/16
下载{$ArticleTitle}原创论文样式
eadLine()) != null) 
   out1.println(lineCount++ + ":" + s); 
   out1.close(); 
   in4.close(); 
 } 
 catch(EOFException ex){ 
  System.out.println("End of stream"); 
 } 
 //5. 数据的存储和恢复 
 try{ 
  DataOutputStream out2 =new DataOutputStream(new BufferedOutputStream( 
   new FileOutputStream("F:\\nepalon\\ Data.txt"))); 
  out2.writeDouble(3.1415926); 
  out2.writeChars("\nThas was pi:writeChars\n"); 
  out2.writeBytes("Thas was pi:writeByte\n"); 
  out2.close(); 
  DataInputStream in5 =new DataInputStream( 
   new BufferedInputStream(new FileInputStream("F:\\nepalon\\ Data.txt"))); 
   BufferedReader in5br =new BufferedReader(new InputStreamReader(in5)); 
   System.out.println(in5.readDouble()); 
   System.out.println(in5br.readLine()); 
   System.out.println(in5br.readLine()); 
 } 
 catch(EOFException e){ 
  System.out.println("End of stream"); 
 } 
 //6. 通过RandomAccessFile操作文件 
 RandomAccessFile rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw"); 
 for(int i=0; i <10; i++) 
  rf.writeDouble(i*1.414); 
  rf.close(); 
  rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r"); 
  for(int i=0; i <10; i++) 
   System.out.println("Value " + i + ":" + rf.readDouble()); 
   rf.close(); 
   rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw"); 
   rf.seek(5*8); 
   rf.writeDouble(47.0001); 
   rf.close(); 
   rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r"); 
   for(int i=0; i <10; i++) 
    System.out.println("Value " + i + ":" + rf.readDouble()); 
    rf.close(); 
  } 
 }  

  关于代码的解释(以区为单位):

  1区中,当读取文件时,先把文件内容读到缓存中,当调用in.readLine()时,再从缓存中以字符的方式读取数据(以下简称“缓存字节读取方式”)。

  1b区中,由于想以缓存字节读取方式从标准IO(键盘)中读取数据,所以要先把标准IO(System.in)转换成字符导向的stream,再进行BufferedReader封装。

  2区中,要以字符的形式从一个String对象中读取数据,所以要产生一个StringReader类型的stream。 

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号