小記 —— IO的String和Byte轉換

1、轉換方法:

//string 轉 byte[]

String str = "Hello";

byte[] srtbyte = str.getBytes();

// byte[] 轉 string

String res = new String(srtbyte);

2、注意事項:編碼格式的設置。

String.getBytes()和new String()都可以設置編碼格式。也就意味着當編碼格式不一致時,會存在亂碼的風險。

    /**
     * Encodes this {@code String} into a sequence of bytes using the given
     * {@linkplain java.nio.charset.Charset charset}, storing the result into a
     * new byte array.
     *
     * <p> This method always replaces malformed-input and unmappable-character
     * sequences with this charset's default replacement byte array.  The
     * {@link java.nio.charset.CharsetEncoder} class should be used when more
     * control over the encoding process is required.
     *
     * @param  charset
     *         The {@linkplain java.nio.charset.Charset} to be used to encode
     *         the {@code String}
     *
     * @return  The resultant byte array
     *
     * @since  1.6
     */
    public byte[] getBytes(Charset charset) {
        if (charset == null) throw new NullPointerException();
        return StringCoding.encode(charset, value, 0, value.length);
    }

    /**
     * Constructs a new {@code String} by decoding the specified array of bytes
     * using the specified {@linkplain java.nio.charset.Charset charset}.  The
     * length of the new {@code String} is a function of the charset, and hence
     * may not be equal to the length of the byte array.
     *
     * <p> The behavior of this constructor when the given bytes are not valid
     * in the given charset is unspecified.  The {@link
     * java.nio.charset.CharsetDecoder} class should be used when more control
     * over the decoding process is required.
     *
     * @param  bytes
     *         The bytes to be decoded into characters
     *
     * @param  charsetName
     *         The name of a supported {@linkplain java.nio.charset.Charset
     *         charset}
     *
     * @throws  UnsupportedEncodingException
     *          If the named charset is not supported
     *
     * @since  JDK1.1
     */
    public String(byte bytes[], String charsetName)
            throws UnsupportedEncodingException {
        this(bytes, 0, bytes.length, charsetName);
    }

3、結合IO流看String和Byte轉換:

文件:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class StringRead {
    public static void main(String[] args) {
        File file = new File("src\\IO\\test.txt");

        try {
            InputStream inputStream = new FileInputStream(file);
            byte[] b = new byte[99];
            int result = inputStream.read(b);
            System.out.println(new String(b,"utf-8"));
            System.out.println(Arrays.toString(b));
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

結果:

測試數據測試數據測試數據測試數據測試數據測試數據測試數據測試數據測
[-26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117, -24, -81, -107, -26, -107, -80, -26, -115, -82, -26, -75, -117]
99

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章