beizili

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

更多内容请关注微信公众号“外里科技

官方公众号外里科技
运营公众号英雄赚
微信wxid_8awklmbh1fzm22
QQ1247408032
开源代码https://gitee.com/B_T/beimi

 

posted on 2019-09-27 11:10  被子里  阅读(16)  评论(0)    收藏  举报  来源