Other java IO streams and javaio streams

Source: Internet
Author: User

Other java IO streams and javaio streams
1. Memory Operation stream (ByteArrayInputStream, ByteArrayOutputStream)

(1 ),Public class ByteArrayInputStream extends InputStream;

ByteArrayInputSteam: this class is a subclass of InputStream. It reads data from the byte array in memory, so its data source is a byte array. The constructor of this class includes:
ByteArrayInputStream (byte [] buf) -------- the parameter buf specifies the data source of the byte array type.
ByteArrayInputStream (byte [] buf, int offset, int lenght) ----- The buf parameter specifies the data source of the byte array type, and the offset parameter specifies the starting subscript position for reading data from the array, lenght specifies the number of bytes read from the array.
The ByteArrayInputStream class uses the adapter design mode. It converts the byte array type to the input stream type so that the program can read the byte array.

public static void byteArrTest() throws IOException{        ByteArrayInputStream bis=new ByteArrayInputStream("abcd".getBytes());        int len=0;        while((len=bis.read())!=-1){            System.out.println((char)len+"<=>"+len);        }        bis.close();    }

(2) public class ByteArrayOutputStream extends OutputStream.

ByteArrayOutputStream: This class implements an output stream, where data is written into a byte array. The buffer will automatically increase as data is constantly written. AvailabletoByteArray()AndtoString()Obtain data. CloseByteArrayOutputStreamInvalid. Methods In this class can still be called after the stream is closed without generating anyIOException.

1. public byte []ToByteArray() Create a newly allocated byte array. The size is the current size of the output stream, and the valid content of the buffer has been copied to the array.

Public static void byteArrOut () throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream (); bos. write (97); bos. write (new byte [] {98,99, 100}); // public byte [] toByteArray () creates a newly allocated byte array. The size is the current size of the output stream, and the valid content of the buffer has been copied to the array. Byte [] bys = bos. toByteArray (); for (byte B: bys) {System. out. println (B + "<=>" + (char) B );}}


2. public StringToString() Use the default Character Set of the platform to convert the buffer content into strings by decoding bytes. NewStringIs a character set function, so it may not be equal to the buffer size.

Public static void byteArrOut2 () throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream (); bos. write (97); bos. write (new byte [] {98,99, 100}); // public String toString () uses the default Character Set of the platform to convert the buffer content to a String by decoding bytes. The length of the new String is a function of the character set, so it may not be equal to the buffer size. String str = bos. toString (); System. out. println (str); // abcd}

(3) Use ByteArrayInputStream and ByteArrayOutputStream together

public static void byteArrOut() throws IOException{        ByteArrayOutputStream bos=new ByteArrayOutputStream();        bos.write(97);        bos.write(new byte[]{98,99,100});        byte [] bys=bos.toByteArray();        ByteArrayInputStream bis=new ByteArrayInputStream(bys);        int len=0;        while((len=bis.read())!=-1){            System.out.print((char)len);//abcd        }    }
Ii. Print stream (PrintStream, PrintWriter)

PrintStream is a subclass of FilterOutputStream.

PrintWriter is a subclass of Writer.

Features of the print stream:
* A: Only data is written and not read. You can only operate on the destination, not the data source. Print the stream only supports byte print stream PrintStream and character print stream PrintWriter
* B: Data of any type can be operated. Their print () and println () methods can operate any type of data.
* C: If Automatic refresh is enabled, it can be automatically refreshed. However, you must call the println () method.

PrintWriter pw = new PrintWriter (new FileWriter ("pw2.txt"), true );

PrintStream ps = new PrintStream (new FileOutputStream ("a.txt"), true );
* D: The stream can directly operate text files.

PublicPrintStream(File file) throws FileNotFoundException.

PublicPrintWriter(File file) throws FileNotFoundException.

1. Use PrintStream to copy files

Public static void streamCopy () throws IOException {BufferedInputStream bis = new BufferedInputStream (new FileInputStream ("a.txt"); // public PrintStream (OutputStream out, boolean autoFlush) use this function to enable the automatic refresh function PrintStream ps = new PrintStream (new FileOutputStream ("Copy. java "), true); byte [] bys = new byte [1024]; int line = 0; while (line = bis. read (bys ))! =-1) {ps. println (new String (bys, 0, line);} ps. close (); bis. close ();}

2. Use PrintWriter to copy files

Public static void copy () throws IOException {BufferedReader br = new BufferedReader (new FileReader ("a.txt"); // encapsulate the destination // public PrintWriter (OutputStream out, boolean autoFlush ); use this function to enable the automatic refresh function. PrintWriter pw = new PrintWriter (new FileWriter ("Copy. java "), true); String line = null; while (line = br. readLine ())! = Null) {pw. println (line);} pw. close (); br. close ();}
3. public final class System) 

Three member variables in the System class

// "Standard" input stream. This stream has been opened and is ready to provide input data. Generally, this stream corresponds to a keyboard input or another input source specified by the host environment or user. Public final static InputStream in = null; // "standard" output stream. This stream is enabled and ready to accept output data. Generally, this stream corresponds to the display output or another output target specified by the host environment or the user. Public final static PrintStream out = null; // "standard" error output stream. This stream is enabled and ready to accept output data. Generally, this stream corresponds to the display output or another public final static PrintStream err = null output target specified by the host environment or user;

With the System. out variable, we can:

// Here we will know that the output statement is essentially an IO stream operation that outputs data to the console. System. out. println ("helloworld"); // obtain the standard output stream object PrintStream ps = System. out; ps. println ("helloworld ");

 

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.