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