Java Io Stream classes in a detailed

Source: Internet
Author: User

A review of the Java stream class; habitual review according to the structure of the graph layer down to learn to review, and finally through the code to realize the impression will be more profound;

About I/O Flow: IO can be understood as the pipeline that Java uses to pass data, and creating an IO is equivalent to connecting a pipeline with a data source.

BYTE stream: the smallest data unit in the data stream is Bytes. Byte: byte is a eight-bit binary number which is a very specific storage space;

Character Stream: the smallest data cell in the data stream is a character; character: is an abstract symbol, such as 1, 2, 3, human, etc. (not to say that the character is transmitted directly, but rather the character is transferred to the corresponding byte stream following the specified encoding Rule)

Java's common input and output streams are actually inherited from 4 abstract classes, namely:

Inputstream,outputstream class based on a single byte (byte-oriented i/o)
Reader based on Double-byte Unicode code unit, writer class (i/o for character Type)

Reader (read in Unicode encoded bytes)

  • CharArrayReader (reading characters from a string array)
    /*CharArrayReader reading characters from a string array*/  public Static voidCharArrayReader ()throwsIOException {Char[] A =New Char[]{' a ', ' b ', ' C '}; CharArrayReader CharArrayReader=NewCharArrayReader (a); /*returns a value of 1 when CharArrayReader is not reading a character*/     inti = 0;  while((i = Chararrayreader.read ())! = 1) {System.out.println ("character:" + (Char) (i); } }

    • InputStreamReader (reading A sequence of characters from a local system File)
    • FileReader (sub-class of Inputstreamreader)
    • StringReader (reading A sequence of characters from a string array)
    • Pipedreader (reading A sequence of characters from a thread pipeline)
  • InputStreamReader (reading data from the input Stream)
     /*about reading a sequence of characters from a local file*/   public Static voidInputreader ()throwsIOException {/*reading data from a file to a data stream*/InputStream InputStream=NewFileInputStream ("e:\\test.txt"); /*convert byte streams to character stream*/InputStreamReader InputStreamReader=NewInputStreamReader (inputstream); Char[]cha =New Char[1024]; intLen =Inputstreamreader.read (cha); System.out.println (NewString (cha,0, len)); /*Off-stream*/Inputstreamreader.close (); }

Writer (write) This doesn't make much peace. reader opposite a write, a read directly on the code:

Code reference: http://blog.csdn.net/lanhuzi9999/article/details/36188047

 /*about reading a sequence of characters from a local file*/   public Static voidInputreader ()throwsIOException {/*reading data from a file to a data stream*/InputStream InputStream=NewFileInputStream ("e:\\test.txt"); /*convert byte streams to character stream*/InputStreamReader InputStreamReader=NewInputStreamReader (inputstream); Char[]cha =New Char[1024]; intLen =Inputstreamreader.read (cha); System.out.println ("local file:" +NewString (cha,0, len)); /*Off-stream*/Inputstreamreader.close (); }  /*Writer*/   public Static voidWriter ()throwsIOException {/*String Writer*/String String= "good Review from today"; StringWriter StringWriter=NewStringWriter (); Stringwriter.write ("how are you?"); Stringwriter.write ("this is written by writer"); System.out.println (string+stringwriter.tostring ()); /*OutputStreamWriter FileWriter*/OutputStream out= system.out;//Print to console//OutputStream out = new FileOutputStream ("d:\\demo.txt");//Print to FileOutputStreamWriter OSR =NewOutputStreamWriter (out);//Output//OutputStreamWriter OSR = new OutputStreamWriter (new fileoutputstream ("d:\\demo.txt"));//Two sentences can be combined into one sentence. //int ch = n;//a//int ch = 20320;//you're//Osr.write (ch); String str = "how are you?" ";//how you doing? Osr.write (str);      Osr.flush ();  Osr.close (); }     public Static voidTranswritebybuf ()throwsIOException {//OutputStream out = system.out;//Print to the Console. OutputStream out =NewFileOutputStream ("d:\\demo.txt");//Print to a file. OutputStreamWriter OSR =NewOutputStreamWriter (out);//Output//OutputStreamWriter OSR = new OutputStreamWriter (new fileoutputstream ("d:\\demo.txt"));//integrated into one sentence. BufferedWriter BUFW =NewBufferedWriter (osr);//Buffering//int ch = n;//a//int ch = 20320;//you're//Osr.write (ch); String str = "how are you?" \ r \ n I'm fine! ";//how you doing? Bufw.write (str);        Bufw.flush ();        Bufw.close (); }

File class (focus On)

  • The object of the file class represents the files on the disk or the file directory
  • File class directly processes files and file systems
  • The file class provides methods for manipulating files or file directories
  • File Common methods
  • Too lazy to write a direct copy paste Source: http://www.jb51.net/article/36126.htm

    Create a method

    1.boolean CreateNewFile () does not exist return true existence returns false
    2.boolean mkdir () Creating a directory
    3.boolean mkdirs () Create a multilevel directory

    Delete method

    1.boolean Delete ()
    2.boolean deleteonexit () file is deleted after use is complete

  • Judging method

    1.boolean CanExecute () Determine if the file is executable
    2.boolean CanRead () Determine if the file is readable
    3.boolean CanWrite () Determine if the file is writable
    4.boolean exists () determine if the file exists
    5.boolean isdirectory ()
    6.boolean Isfile ()
    7.boolean Ishidden ()
    8.boolean Isabsolute () Determines whether an absolute path file does not exist or can be judged

    Get method

    1.String GetName ()
    2.String GetPath ()
    3.String GetAbsolutePath ()
    4.String getParent ()//if No parent directory returns null
    5.long LastModified ()//gets The last modified time
    6.long length ()
    7.boolean Renameto (File F)
    8.file[] liseroots ()//get Machine Drive letter
    9.string[] List ()
    10.string[] List (filenamefilter Filter)

  • Common method implementation
  •  /*File class*/   public Static  voidFile ()throwsIOException {System.out.println ("woca"); /*Create a path*/File File3=NewFile ("e://file//jj");      System.out.println (file3.mkdir ()); /*Create a file*/File File=NewFile ("e://te.txt");      File.createnewfile (); /*to create a file based on a base path*/File file2=NewFile (file3, "tt.txt");      File2.createnewfile (); /*determine if the file object is a document or a path*/System.out.println ("is path" +file3.isdirectory ()); System.out.println ("is file" +File2.isfile ()); /*get the directory of files within a folder*/File file4=NewFile ("e://workspace"); String names[]=file4.list ();  for(String name:names) {System.out.println (name); }            /*Deleting Files*/File2.delete (); }

Java Io Stream classes in a detailed

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.