The "in" Java Stream Classic

Source: Internet
Author: User
Tags readline stdin
Stream
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 ());   &nbsp} 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 ();   &NBSP} 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 is PI");      out2.close ();  &Nbsp;   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 ());   &nbsp} catch (Eofexception e) {      system.out.println ("End of Stream");    }      //6. Reading and writing random access   //files are the same as before.   //(not repeated here)      &NBSP} catch (FileNotFoundException e) {     system.out.println (       "File not Found: + args[1]);   &nbsp} catch (IOException e) {      system.out.println ("IO Exception");    }&nbsp}}///:~


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.