Receive information (processing Chinese garbled) by byte stream and character streams respectively

Source: Internet
Author: User

First, create the file ShowMsg.txt, and set the encoding to UTF-8, and run the following code.

Package com.clogi.test;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.PrintWriter;

public class Testmsg {

public static void Main (string[] args) {

FileOutputStream fos = null;
File file = null;
try {
File = new file ("E://showmsg.txt");
if (file.exists ()) {
File.delete ();
}
FOS = new FileOutputStream (file,true);
byte[] byt = new byte[1024];
StringBuffer strbuf = new StringBuffer ();
Strbuf.append ("The 10 words that gates left to workplace workers:");
Strbuf.append ("1"). Society is full of injustice. You don't want to change it, you can only adapt it first. (Because you can't control it) "+
"2. The world does not care about your self-esteem, people only see your achievements." Never overemphasize your self-esteem until you have achieved it. (Because the more you emphasize self-esteem, the more you are disadvantaged) ");
String str = strbuf.tostring ();
Byt = Str.getbytes ("UTF-8");//This place the encoded format for the content written to the file is UTF-8
Fos.write (BYT);
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}finally{
if (null!= fos) {
try {
Fos.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}

}
}

Two. E://showmsg.txt this has been created. Now we're going to read the contents of this file in the program. I wrote 2 ways to read, though all very simple, written to deepen the impression.

1. Receive information in the form of character streams

Package com.clogi.test;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStreamReader;

public class ShowMsg {

/*
* Receive information in the form of character streams
*/

public static void Main (string[] args) {
FileInputStream FIS = null;
BufferedReader br = null;
try {

To read from a file, the encoding format must be the same as when the content was written.
For example, the encoding specified when writing is UTF-8, so it should be UTF-8 when reading content in this place.
br = new BufferedReader (New InputStreamReader (
New FileInputStream (New File ("E://showmsg.txt")), "UTF-8");

String STRs = null;
String str = NULL;
StringBuffer SBF = new StringBuffer ();
while (Br.read ()!=-1) {
Sbf.append (Br.readline ());
Sbf.append ("n");/linefeed
}
str = sbf.tostring ();
byte[] bytes = str.getbytes ()//If the specified encoding format does not work in this place. It is best to specify the encoding when reading the stream (for example, above).
STRs = new String (bytes);

System.out.println (STRs);

catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
finally {
if (null!= FIS) {
try {
Fis.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}
}
Run this main () method to see the console print information.

2. Receive information in the form of a stream of bytes.

Package com.clogi.test;

Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;

public class Showmsgbuf {

/*
* Receive information in the form of a byte stream.
* It's worth noting that you have to encode the encoding to the same encoding as the file you want to read so that there is no garbled text.
*/
public static void Main (string[] args) {

Bufferedinputstream bis = null;
Byte[] by = new byte[10240000];//length can be appropriately enlarged or decreased depending on the length of the content.
try {
bis = new Bufferedinputstream (New FileInputStream new File (
"E://showmsg.txt"));/Read E://bug.txt this file.
int byint =-1;
int w = 0;
while ((Byint = Bis.read ())!=-1) {
BY[W] = (byte) byint;//converts the read integer to the byte type and puts this byte into a byte array.
w++;
}
String strmsg = new String (by, "UTF-8");//This step is important to convert a byte array to a string and specify the encoding.
String returnmsg = Strmsg.trim ();//Remove the space after the string to avoid using a space instead of filling the byte array.
System.out.println (RETURNMSG);
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
finally {
if (null!= bis) {
try {
Bis.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}

}

}
Running this main () method also allows you to see the information printed on the console.

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.