Summary and solutions of scrambled code problems in java

  • 2020-05-16 06:58:19
  • OfStack

The garbled code problem in java

Recently, I often encountered the problem of scrambled codes in Java when I was working on projects. Therefore, I took the time to sort out the situation of scrambled codes and how to deal with them. Here is a sorting.

Analysis of the

Encoding and decoding

Encoding is converting a character into a byte, and decoding is converting a byte into a character.

Byte stream and character stream

Read and write operations to files are performed through a byte stream. Even if there is a stream of characters in JAVA, the underlying byte stream is still used.

Garbled code problem appears

The most frequently used character in java is when we read a file into memory and display it in the console (byte stream -- > Character stream), which requires decoding. If the file is UTF-8 encoded and we decode it incorrectly as GBK(if we do not specify the encoding,JAVA will adopt the system default encoding), then only the scrambled code will be displayed. When we write a file, we'd better specify the code (UTF-8).

The solution

Example 1

When converting a byte stream to a character stream, we specify the encoding format. This is our file should also be gb2312 code


public static String read(String filename) throws Exception {
    InputStream is = new FileInputStream(filename);
    BufferedReader in = new BufferedReader(new InputStreamReader(is,
        "gb2312"));           // Specified encoding format 
    String s;
    StringBuilder sb = new StringBuilder();
    while ((s = in.readLine()) != null) {
      sb.append(s + "\n");
    }
    in.close();
    return sb.toString();
}

Example 2

It is read directly through the byte stream and when converted to a character using String, the encoding is specified.


package com.dy.xidian;

import java.io.FileInputStream;
import java.io.InputStream;

class BufferedInputFile {
  public static String read(String filename) throws Exception {
    @SuppressWarnings("resource")
    InputStream is = new FileInputStream(filename);

    byte[] b = new byte[1024];
    is.read(b);
    return new String(b, "gb2312");
  }
}

public class MemoryInput {
  public static void main(String[] args) throws Exception {
    String filename = "E:/html/gb2312.php";
    String s = BufferedInputFile.read(filename);
    System.out.println(s);
  }
}

trap

The I/O operation has an FileReader class, which hides the details of the byte flow into the character stream, so we can use it this way. BufferedReader in = new BufferedReader(new FileReader(filename)); So, we're just going to get a stream of characters. However, we found that we did not set the encoding because the default encoding was used in FileReader. This becomes dangerous if the default encoding format is different from the encoding of our file, then the data 1 will be read out of order. So it's best to do the flow conversion the way we did in the example.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: