• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

Think in java Stream 经典

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 14次 | 进入软件测试论坛讨论

领测软件测试网
import java.io.*;

public class NewIODemo {public static void main(String[] args) { try {   // 1. Reading input by lines:   BufferedReader in =   new BufferedReader(   new FileReader(args[0]));   String s, s2 = new String();   while((s = in.readLine())!= null)   s2 += s + "\n";   in.close();    // 1b. Reading standard input:   BufferedReader stdin =    new BufferedReader(     new InputStreamReader(System.in));    System.out.print("Enter a line:");   System.out.println(stdin.readLine());      // 2. Input from memory   StringReader in2 = new StringReader(s2);   int c;   while((c = in2.read()) != -1)    System.out.print((char)c);      // 3. Formatted memory input   try {     DataInputStream in3 =     new DataInputStream(     // Oops: must use deprecated class:     new StringBufferInputStream(s2));     while(true)      System.out.print((char)in3.readByte());   } catch(EOFException e) {      System.out.println("End of stream");   }      // 4. Line numbering & file output   try {     LineNumberReader li =      new LineNumberReader(       new StringReader(s2));     BufferedReader in4 =      new BufferedReader(li);     PrintWriter out1 =      new PrintWriter(       new BufferedWriter(        new FileWriter("IODemo.out")));     while((s = in4.readLine()) != null )     out1.println(      "Line " + li.getLineNumber() + s);     out1.close();   } catch(EOFException e) {     System.out.println("End of stream");   }      // 5. Storing & recovering data   try {     DataOutputStream out2 =     new DataOutputStream(       new BufferedOutputStream(         new FileOutputStream("Data.txt")));     out2.writeDouble(3.14159);     out2.writeBytes("That was pi");     out2.close();     DataInputStream in5 =     new DataInputStream(      new BufferedInputStream(       new FileInputStream("Data.txt")));     BufferedReader in5br =       new BufferedReader(         new InputStreamReader(in5));     // Must use DataInputStream for data:     System.out.println(in5.readDouble());     // Can now use the "proper" readLine():     System.out.println(in5br.readLine());   } catch(EOFException e) {     System.out.println("End of stream");   }      // 6. Reading and writing random access   // files is the same as before.   // (not repeated here)      } catch(FileNotFoundException e) {     System.out.println(     "File Not Found:" + args[1]);   } catch(IOException e) {     System.out.println("IO Exception");   } }} ///:~

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网