Java input/output stream system and java output stream system

Source: Internet
Author: User

Java input/output stream system and java output stream system

When I use java io streams to read and write files, it is always confusing by its various streams. There are more than 40 classes, which have been sorted out. After a while, it will be messy. I decided to sort them out! In case you forget

Java input/output stream system

1. byte stream and byte stream

Byte stream: Read by byte. Refer stream: Read by character.

The byte stream is easy to read and has powerful functions. When the byte stream is not available, you can use Word throttling.

Byte stream base class: InputStream and OutputStream

InputStream method:

 

OutputStream method:

 

Stream base class: Reader and Writer

Reader Method:

 

Writer method:

 

2. Input/output stream system

 

3. Conversion stream

InputStreamReader: byte input stream-> character input stream conversion

OutputStreamWriter: byte output stream-> character output stream conversion

For example: byte input stream> character input stream conversion

It is not convenient to use byte streams, but it is not convenient to read data from common readers. Convert the data into BufferReader and read a row at a time using the Read line () of BufferReader.

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));                String result = "";                String line = "";               while((line = reader.readLine())!= null){                   result = result+ line;               }               System.out.println(result);

InputStream to String:

Private static String changeInputeStream (InputStream inputStream, String encode) {// It is usually called a memory stream, written in the memory ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); byte [] data = new byte [1024]; int len = 0; String result = ""; if (inputStream! = Null) {try {while (len = inputStream. read (data ))! =-1) {outputStream. write (data, 0, len);} // result is the result = new String (outputStream. toByteArray (), encode); outputStream. flush (); outputStream. close (); inputStream. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} return result ;}

 

InputStream-> StringBuffer:

StringBuffer buffer = new StringBuffer (); InputStreamReader reader = new InputStreamReader (request. getInputStream (), "UTF-8"); char [] requestchar = new char [5]; if (reader! = Null) {System. out. println ("default encoding:" + reader. getEncoding (); // default encoding: UTF8} // buffer reader. readLine () while (temp = reader. read (requestchar ))! =-1) {buffer. append (requestchar, 0, temp); System. out. println (buffer. toString ();} String result = buffer. toString ();

4. file read/write:

Class Kaoshi {public static void main (String [] args) {role in = new role (System. in); int score = 0; // score; StringBuffer answer = new StringBuffer (); // stores the user's answer, dynamic String; String result = "ACDD "; // correct answer; try {FileReader file = new FileReader ("Test.txt"); // open the file; BufferedReader intw = new BufferedReader (file); // buffer the stream, read by row; String s = null; while (s = intw. readLine ())! = Null) {// the judgment is not empty; if (! S. startsWith ("*") {System. out. println (s);} else {System. out. println ("enter the correct answer:"); String str = in. next (); char c = str. charAt (0); // collect answers; answer. append (c) ;}} catch (IOException e) {e. printStackTrace () ;}for (int I = 0; I <result. length (); I ++) {if (result. charAt (I) = answer. charAt (I) | result. charAt (I) = answer. charAt (I)-32) {score + = 25 ;}} System. out. println (score );}}

 

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.