Java InputStream Read file garbled problem __ Big Data

Source: Internet
Author: User
Tags getmessage readfile

New people began to learn to write blog, do not like to spray. Welcome to the big guys to criticize, appreciate disrespect.

I did 10 months ago in C #, and later to the Java self-study also has a period of time, there is the basis of object-oriented learning is very fast. Today suddenly whim, want to operate the Java file read (have to say before they are very lazy, can Baidu things themselves do not want to do their own hands, hope that the vast numbers of friends do not learn me, Do everything yourself more practice) This article mainly records itself in the use of InputStream encountered problems (garbled and byte) nonsense not to say more directly on the code

Class ReadFile {
public string Readfilebyinputstream (string filePath) {
String result= "";
File File=new file (FilePath);
InputStream Inputstream=null;
byte []b=new byte[1024];//accepts 1024 bytes
try {
Inputstream=new fileinputstream (file);
int temp;
int length=0;
while ((Temp=inputstream.read ())!=-1) {//read one byte at a time, stored in a byte array
b[length]= (byte) temp;
length++;
}
return new string (b,0,length);//Convert bytes to string
}
catch (Exception e) {
E.printstacktrace ();
return E.getmessage ();
}
finally {
if (inputstream!=null) {
try {
Inputstream.close ();
catch (IOException e) {
E.printstacktrace ();
}
}

}
}

}

Code calls:

public static ReadFile filetool=new ReadFile ();
public static void Main (string[] args) {
System.out.println (Filetool.readfilebyinputstream ("D:/filetest/test.txt"));
}

Output content:

Ljflsjafl
Asfjliruqwdkaopsj
ʹʱ䷢
AKDLASJFLNF Flaw Iowa 2
42żpsdaksfasjsffas

Code Analysis:

Garbled, this is because the document on our own computer default GBK format, and my. java file by default to Utf-8 file, so there are garbled Chinese,

return new String (b,0,length) is changed to return new string (B,0,length, "GBK") to specify the full output of the encoding format in Chinese. As follows:

Ljflsjafl
Asfjliruqwdkaopsj
Delivery time Issued
AKDLASJFLNF fermented flaw Iowa Man 2

42 even Psdaksfasjsffas

But there is also a situation may be garbled, such as I changed the code to look like, I set the byte array smaller such as 11 bytes, and then the loop users of these 11 byte array to accept the read data, each Read full 11 bytes converted to string type; The code is as follows

Class ReadFile {
public string Readfilebyinputstream (string filePath) {
String result= "";
File File=new file (FilePath);
InputStream Inputstream=null;
byte []b=new byte[11];//byte array size
try {
Inputstream=new fileinputstream (file);
int temp;
int length=0;
while ((Temp=inputstream.read ())!=-1) {//read one byte at a time, stored in a byte array
Reads 11 bytes at a time and converts to string
if (length==11) {
Result+=new String (b,0,length, "GBK");
length=0;
}
b[length]= (byte) temp;
length++;
}
Return result+=new String (b,0,length, "GBK");//exit loop, the last byte array may not have 11 bytes
}
catch (Exception e) {
E.printstacktrace ();
return E.getmessage ();
}
finally {
if (inputstream!=null) {
try {
Inputstream.close ();
catch (IOException e) {
E.printstacktrace ();
}
}

}
}

}

This time output: LJFLSJAFL
Asfjliruqwdkaopsj
Send its utensils Ben Zhuo Shui
AKDLASJFLNF fermented flaw Iowa Man 2

42 even Psdaksfasjsffas

Conclusion:

Still appear garbled, this is because we Chinese occupies two bytes, for example such a text content "ABCDEFGHJK Hu" Front is 10 letters, 11th is Chinese, in fact this text occupies 12 bytes, on the surface occupies 11 bytes Just, the last Chinese "Hu" will be decomposed into two bytes, When I use 11 bytes to get this text, only read the first 10 letters + a Chinese byte, and then use the new String (B,0,length, "GBK") to transfer to the Chinese when the natural garbled.

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.