InputStream conversion InputStreamReader re-conversion BufferedReader

Source: Internet
Author: User
Tags knowledge base

The end is generally converted into BufferedReader to read the string

InputStream instream = Request.getinputstream ();
BufferedReader br = new BufferedReader (new InputStreamReader (instream));

. InputStream, OutputStream

Abstract class for processing byte streams

InputStream is a superclass of all classes of byte input streams, generally we use its subclasses, such as FileInputStream.

OutputStream is a superclass of all classes of byte output streams, generally we use its subclasses, such as FileOutputStream.

2.InputStreamReader OutputStreamWriter

Abstract class for handling character streams

InputStreamReader is a bridge of byte flow to a character stream, which converts a stream of bytes into a character stream.

OutputStreamWriter is a bridge of character flow to a byte stream, which converts a stream of characters into a byte stream.

3.BufferedReader BufferedWriter

The BufferedReader is extended by the reader class, providing a common buffered way to read text, ReadLine reading a line of text,

Reads text from the character input stream, buffering individual characters, providing efficient reading of characters, arrays, and rows.

The BufferedWriter is extended by the writer class, providing a common buffer for text writing, newline using the platform's own line delimiter,

Writes text to the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings.

InputStream can read a byte from the source,
So it's the lowest level,
Cases:

[Java]View PlainCopy
  1. Import <a href="HTTP://LIB.CSDN.NET/BASE/17" class=' Replace_word ' title="Java ee Knowledge Base" target=' _blank ' style=' color: #df3434; font-weight:bold; '     >Java</a>.io.*;
  2. Public class Main {
  3. public static void Main (string[] args) {
  4. try {
  5. FileInputStream fis=New FileInputStream ("D://desktop//test.txt");
  6. int i;
  7. try {
  8. While ((I=fis.read ())! =-1) {
  9. System.out.println (i);
  10. }
  11. / * The Test.txt file has only one word "Chan" and is encoded as UTF8
  12. * Input Output
  13. * 233
  14. 153
  15. 179
  16. */
  17. } catch (IOException e) {
  18. //TODO auto-generated catch block
  19. E.printstacktrace ();
  20. }
  21. } catch (FileNotFoundException e) {
  22. //TODO auto-generated catch block
  23. E.printstacktrace ();
  24. }
  25. }
  26. }

InputStreamReader
InputStreamReader sealed the InputStream inside,
It reads one character at a time in a higher-level way,
The following example assumes that there is a document encoded as UTF8,
It has only one Chinese character, "Chan".

[Java]View PlainCopy
  1. Import java.io.*;
  2. Public class Main {
  3. public static void Main (string[] args) throws FileNotFoundException, unsupportedencodingexception {
  4. FileInputStream fis=New FileInputStream ("D://desktop//test.txt");
  5. try {
  6. InputStreamReader isr=New InputStreamReader (FIS,"UTF8");
  7. int i;
  8. While ((I=isr.read ())! =-1) {
  9. System.out.println ((char) i); //Output chan
  10. }
  11. } catch (Exception e) {
  12. //TODO auto-generated catch block
  13. E.printstacktrace ();
  14. }
  15. }
  16. }

BufferedReader
BufferedReader is more advanced than InputStreamReader,
It sealed the StreamReader class,
Read a row of characters at a time

[Java]View PlainCopy
  1. Import java.io.*;
  2. Public class Main {
  3. public static void Main (string[] args) throws FileNotFoundException, unsupportedencodingexception {
  4. FileInputStream fis=New FileInputStream ("D://desktop//test.txt");
  5. try {
  6. InputStreamReader isr=New InputStreamReader (FIS,"UTF8");
  7. BufferedReader br=New BufferedReader (ISR);
  8. String Line;
  9. While ((Line=br.readline ()) = null) {
  10. System.out.println (line);
  11. }
  12. } catch (Exception e) {
  13. //TODO auto-generated catch block
  14. E.printstacktrace ();
  15. }
  16. }
  17. }

InputStream conversion InputStreamReader re-conversion BufferedReader

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.