Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Java IO Stream.经典

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

又重新看了一下Think In Java 里IO这部分,里面的一个例程很经典。

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");
   }
 }
} ///:~




相关文章

相关软件