Typical applications of Java IO streams

Source: Internet
Author: User
Tags readline value of pi

Although there are a large number of Io stream classes in the library, they can be grouped together in many different ways, but there are actually only a few ways to use them frequently. However, care must be given to get the right combination. The following fairly lengthy example shows the creation and use of a typical IO configuration, which can be used as a reference when writing your own code. Note that each configuration begins with a numbered annotation and provides appropriate explanatory information.
 

: Iostreamdemo.java//Typical IO Stream configurations import java.io.*;

Import com.bruceeckel.tools.*; public class Iostreamdemo {public static void main (string[] args) {try {//1.
            Buffered input file DataInputStream in = new DataInputStream (New Bufferedinputstream (
      New FileInputStream (Args[0]));
      string s, s2 = new string ();
      while (s = in.readline ())!= null) s2 + = + + "\ n";

      In.close (); 2.
      Input from memory StringBufferInputStream in2 = new StringBufferInputStream (s2);
      int C;

      while ((c = in2.read ())!=-1) System.out.print ((char) c); 3. Formatted memory input try {datainputstream in3 = new DataInputStream (New Stringbuff
        Erinputstream (S2));
      while (true) System.out.print ((char) in3.readbyte ());
      catch (Eofexception e) {System.out.println ("End of Stream encountered"); }

      4.
            Line numbering & file output try {linenumberinputstream li = new Linenumberinputstream (
        New StringBufferInputStream (S2));
        DataInputStream in4 = new DataInputStream (LI);
                PrintStream out1 = new PrintStream (new Bufferedoutputstream, New FileOutputStream (
        "Iodemo.out"));
        while (s = in4.readline ())!= null) out1.println ("line" + li.getlinenumber () + s); Out1.close ();
      Finalize () not reliable!
      catch (Eofexception e) {System.out.println ("End of Stream encountered"); }//5. Storing & Recovering data try {dataoutputstream out2 = new DataOutputStream (new
        Bufferedoutputstream (New FileOutputStream ("Data.txt"));
        Out2.writebytes ("Here's The value of pi: \ n");
        Out2.writedouble (3.14159); out2.clOSE (); DataInputStream in5 = new DataInputStream (new Bufferedinputstream (New Fileinputstrea
        M ("Data.txt"));
        System.out.println (In5.readline ());
      System.out.println (In5.readdouble ());
      catch (Eofexception e) {System.out.println ("End of Stream encountered"); }//6.
      reading/writing Random access files Randomaccessfile RF = new Randomaccessfile ("Rtest.dat", "RW");
      for (int i = 0; i < i++) rf.writedouble (i*1.414);

      Rf.close ();
      RF = new Randomaccessfile ("Rtest.dat", "RW");
      Rf.seek (5*8);
      Rf.writedouble (47.0001);

      Rf.close ();
      RF = new Randomaccessfile ("Rtest.dat", "R");
      for (int i = 0; i < i++) System.out.println ("Value" + i + ":" + rf.readdouble ());

      Rf.close (); 7.
      File input Shorthand InFile in6 = new InFile (args[0)); String s3 = new String ();
        System.out.println ("in file:" + in6.readline ());

      In6.close (); 8.
      Formatted file output shorthand printfile out3 = new Printfile ("Data2.txt");
      Out3.print ("Test of Printfile");

      Out3.close (); 9.
      Data file output shorthand outfile Out4 = new OutFile ("Data3.txt");
      Out4.writebytes ("Test of outdatafile\n\r");
      Out4.writechars ("Test of outdatafile\n\r");

    Out4.close ();
    catch (FileNotFoundException e) {System.out.println ("File not Found: + args[0]);"
    catch (IOException e) {System.out.println ("IO Exception"); }
  }
} ///:~
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.