Read the byte stream and byte stream of the website

Source: Internet
Author: User

Method for converting an input stream to a string:


When reading a website, the method for reading Chinese characters is POST)



String sendurl = "http://www.baidu.com ";

URL url = new URL (sendurl); // actual url

URLConnection connection = url. openConnection ();

Connection. setDoOutput (true );


// The connection settings can be found here.


OutputStreamWriter out = null;

Out = new OutputStreamWriter (connection. getOutputStream (), "GB2312"); // set the GB2312 Parameter

String writerStr = "post content, with & connection Parameters ";

Out. write (writerStr );

Out. flush ();

Out. close ();


Method 1: Reading non-Chinese DataInputStream won't work.

DataInputStream in = new DataInputStream (connection. getInputStream ());

String inline = "";

StringBuffer getResult = new StringBuffer ();

While (inline = in. readLine ())! = Null ){

GetResult. append (inline );

}


BufferedReader tBufferedReader = new BufferedReader (new InputStreamReader (connection. getInputStream ()));

StringBuffer getResult = new StringBuffer ();

String sTempOneLine = new String ("");

While (sTempOneLine = tBufferedReader. readLine ())! = Null ){

GetResult. append (sTempOneLine );

}


Method 2: read Chinese Characters

InputStreamReader tBufferedReader = new InputStreamReader (connection. getInputStream (), "GB2312 ");

Int ch;

StringBuffer getResult = new StringBuffer ();

While (ch = tBufferedReader. read ()>-1 ){

GetResult. append (char) ch );

}

TBufferedReader. close ();



Output String result = getResult. toString ();

System. out. println (result );


When InputStreamReader is created, it is only available when the character encoding format is set to "GB2312". Through testing, some websites, such as the Baidu homepage, cannot write "GB2312". Specific Problem Analysis


The problem with the stream is that the byte stream and the byte stream are unclear:

The byte stream is transmitted in 8 bits, and the byte stream is transmitted in 16 bits.


Byte stream --> when bytes stream byte [] is converted to String)

String bytetoStr new String (byte [] bytes, String charSetName );


Byte stream --> byte stream String converted to byte [])

Byte [] strtoByte = String. getBytes (String charsetName );


The byte stream does not use the cache area, while the byte stream uses the cache area, which saves the memory space. Note that when you use the batch stream to operate a file, you must disable or clear the cache when writing the file because the string is still in the cache area. So we can see when out. flush () is used.


Generally, it is best to use byte streams when encode operations are not involved.


This article from the "calf" blog, please be sure to keep this source http://xiaoniudu.blog.51cto.com/7952142/1300063

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.