Java Learning Note: Java streaming

Source: Internet
Author: User
Tags object object

First, Introduction

The java.io package contains almost all the required classes for operation input and output. All of these flow classes represent the input source and output destination.

The streams in the Java.io package support many formats, such as basic types, objects, localized character sets, and so on.

A stream can be understood as a sequence of data. The input stream represents reading data from one source, and the output stream represents writing data to a target.

Second, notes

1.IO

ImportJava.io.File;/*** IO stream 1.File class: Can operate the specified path of the file, provides the file additions and deletions to change * * * */ Public classIO { Public Static voidMain (string[] args) {Try {            //test ();            NewIO (). Check ("d:\\"); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); }    }     Public Static voidTest ()throwsException {String path= "D:\\xx; //View Files//get the file path method (you can see the effect in the network path)File File =NULL; File=NewFile (path); //System.out.println (File.getabsolutepath ()); //        //string[] names = File.list (); //For (String string:names) {//System.out.println (string); // }        //gets the file object for all files under the current path//file[] Files=file.listfiles (); //For (File f:files) {//System.out.println (F.getpath ()); //System.out.println (F.getparent ()); // }        //Create a file//path+= "\\a.txt"; //file=new file (path); //boolean result=file.createnewfile (); //System.out.println (Result); //Create a folder//path+= "\\text"; //file=new file (path); //boolean result=file.mkdir (); //System.out.println (Result); //To Create a folder recursively//path + = "\\TEXT\\ABC"; //file = new file (path); //Boolean result = File.mkdirs (); //System.out.println (Result); //Rename , and move the pathPath + = "\\abc.txt"; File=NewFile (path); File.renameto (NewFile ("D:\\xx\\abcd.txt")); }     Public voidCheck (String path) {File file=NewFile (path); file[] Files=File.listfiles ();  for(File f:files) {if(F.isfile ()) {System.out.println (F.getabsolutepath ()); }            Else{Check (F.getabsolutepath ()); }        }    }}

2.stream

/*** Stream: can understand the data scrolling, is a data stream 1. By direction (Programmer's Angle): input stream, output stream 2. By processing Unit: Byte stream (Outputstream,inputstream is byte stream), character stream 3. By function: node Stream, * *  * */ Public classStream { Public Static voidMain (string[] args) {Try {            //file (); //ByteArray ();obj (); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); }    }     Public Static voidfile () {Try{FileInputStream InputStream=NewFileInputStream (NewFile ("D:\\xx\\abcd.txt")); FileOutputStream OutputStream=NewFileOutputStream ("D:\\xx\\a.txt"); byte[] bs =New byte[10]; intLen = 0;  while(len = Inputstream.read (BS))! =-1) {                //string msg = new String (BS, 0, Len); //System.out.println (msg);Outputstream.write (BS,0, Len);            } inputstream.close ();        Outputstream.close (); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); }    }    /*** Byte array flow is the smallest unit of flow, can not generally be processed by the use of it * *@throwsIOException*/     Public Static voidByteArray ()throwsIOException {string string= "ABCDEFG"; byte[] bs =string.getbytes (); Bytearrayoutputstream OutputStream=NewBytearrayoutputstream (); Bytearrayinputstream InputStream=NewBytearrayinputstream (BS); intLen = 0; byte[] r =New byte[2]; //While ((Len=inputstream.read ())!=-1) {//the default Read method that reads one byte at a time         while(len = Inputstream.read (r))! = 1) {String msgstring=NewString (r, 0, Len);        System.out.println (msgstring); }    }    //Object Flow     Public Static voidObj ()throwsException {objectoutputstream outputstream=NewObjectOutputStream (NewFileOutputStream ("d:\\ blog \\a.txt")); User User=NewUser ("Tom")); Outputstream.writeobject (user);//If you need to write an object to the stream, you need to implement theOutputstream.flush ();        Outputstream.close (); System.out.println ("OK"); ObjectInputStream InputStream=NewObjectInputStream (NewFileInputStream ("D:\\xx\\a.txt")); Object Object=Inputstream.readobject (); if(ObjectinstanceofUser)            {System.out.println ((User) object). GetName ());        System.out.println ((User) object). Getage ()); }    }    //String Buffer stream     Public Static voidStringBuffer ()throwsException {stringbufferinputstream InputStream=NewStringBufferInputStream ("ABCDE"); intLen = 0; byte[] bs =New byte[1024];  while(len = Inputstream.read (BS))! =-1) {String msg=NewString (BS, 0, Len);        SYSTEM.OUT.PRINTLN (msg); }    }}

3. Character Stream

/*** Character stream: can only handle the text class file, cannot process the picture * Summary * 1. Input stream Output stream * 2. Byte stream (the subclass of Outputstream,inputstream is a byte stream) * character Stream (subclass of reader and writer) * 3. By function: node flow and Processing Liu * * * */ Public classCharacterstream { Public Static voidMain (string[] args) {Try{file (); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); }    }     Public Static voidFile ()throwsException {FileReader reader=NewFileReader (NewFile ("D:\\xx\\abcd.txt")); FileWriter writer=NewFileWriter (NewFile ("D:\\xx\\abcd.txt")); //While (Reader.ready ()) {//System.out.println (Reader.read ()); // }        intLen = 0; Char[] cs =New Char[1024];  while(len = Reader.read (cs))! =-1) {System.out.println (NewString (CS, 0, Len));    } reader.close (); }     Public Static voidBuffer ()throwsException {BufferedReader reader=NewBufferedReader (NewFileReader ("D:\\xx\\abcd.txt")); BufferedWriter writer=NewBufferedWriter (NewFileWriter ("D:\\xx\\abcd.txt")); intLen = 0; Char[] cs =New Char[1024];  while(len = Reader.read (cs))! =-1) {Writer.write (CS,0, Len); }         while(Reader.ready ()) {System.out.println (Reader.readline () )+ "|||");        Writer.write (Reader.readline ());        } reader.close ();    Writer.close (); }}

Java Learning Note: Java streaming

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.