Java input stream [Reader,inputstream] basic stream operation with no buffering effect. and Chinese garbled situation

Source: Internet
Author: User

Reader,inputstream is used to read the contents of a resource, which can be a file or a network

First, there is a TXT file, the contents of the file are as follows

File F = new files ("path"); FileInputStream fis = new FileInputStream (f);//create an input stream, read the contents of F into the program to//SYSTEM.OUT.PRINTLN (Fis.read ()); byte[] B = new byte[1024];//defines an array that holds the read class tolerance int hasread = 0;//used to record how long the read is hasread=fis.read (b); string s = new string (b,0,hasread); System.out.println (s); enthusiastic netizens | 2012-12-04 14:29

If you are using a byte stream:

Package Cn.bean.demo.inoutstream;import Java.io.fileinputstream;import java.io.ioexception;import Java.io.InputStream; Public classFileinputstreamtest { Public Static voidMain (string[] args) {testinputstream (); }     Public Static voidTestinputstream () {Try(InputStreaminch=NewFileInputStream ("Hello.txt");) {
Used to cache byte data for each readbyte[] buffer =New byte[Ten];
The number of bytes to put into the buffer array each timeintLen =0; String s=""; while(len =inch. Read (buffer))! =-1) {s+=NewString (Buffer,0, Len); } System. out. println (s); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}

Console output is garbled:

If you are using a character stream:

 

Package cn.bean.demo.readwrite;

Import Java.io.FileInputStream;
Import Java.io.FileReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.Reader;

public class Readertest {
public static void Main (string[] args) {
This will appear garbled.
Testfilereader ();
Normal output of Chinese
Testinputstreamreader ();
}
public static void Testinputstreamreader () {
, InputStreamReader converts the byte stream to a character stream. is a bridge of bytes flowing to a character stream. If you do not specify a character set encoding, the decoding process uses the default character encoding for the platform, such as: GBK. InputStreamReader constructor Specifies that only InputStream types can be received
Try (InputStream in=new fileinputstream ("Hello.txt");
Reader read=new InputStreamReader (in, "UTF-8");) {

Used to cache character data for each read
Char[] C=new char[10];

The number of characters to put into the buffer array each time
int len=0;
String s= "";
while ((Len=read.read (c))!=-1) {
s+= new String (C,0,len);
}
System.out.println (s);
} catch (Exception e) {
E.printstacktrace ();
}

}
public static void Testfilereader () {

Try (
Reader read=new FileReader ("Hello.txt");) {

Used to cache the characters that are read every time
Char[] C=new char[10];

The number of characters to put in the buffer array each time
int len=0;
String s= "";
while ((Len=read.read (c))!=-1) {
s+= new String (C,0,len);
}
System.out.println (s);
} catch (Exception e) {
E.printstacktrace ();
}

}
}

If you run the Testfilereader method: It will appear garbled:

If it is the Testinputstreamreader method will display normally;

Note the InputStreamReader class can specify the encoding, and the outputstreamwriter of the object is also possible.

The next chapter records the output stream of OutputStream and writer, which is used to output "not including buffered Fileoutputstream,outputstreamwriter and FileWriter" to the file or network.

Java input stream [Reader,inputstream] basic stream operation with no buffering effect. and Chinese garbled situation

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.