Java FileInputStream默认编码方式

Java FileInputStream默认编码方式使用如下代码测试:InputStreamis=newFileInputStream(newFile(“C:\\Users\\Administrator\\Desktop\\test1.txt”));    byte[]bs=newbyte[4096];    intlen=is.read(bs);    System.out.pri

大家好,又见面了,我是你们的朋友全栈君。

使用如下代码测试:

InputStream is = new FileInputStream(new File(“C:\\Users\\Administrator\\Desktop\\test1.txt”));
        byte [] bs = new byte[4096];
        int len = is.read(bs);
        System.out.print(len);
        for(byte b : bs){

            System.out.print(b + “,”);
        }
        System.out.println();
        is = new FileInputStream(new File(“C:\\Users\\Administrator\\Desktop\\test2.txt”));
        bs = new byte[4096];
        len = is.read(bs);
        System.out.print(len);
        for(byte b : bs){

            System.out.print(b + “,”);
        }

其中,test1.txt文件的编码方式为GBK(在简体中文Windows操作系统中,ANSI 编码代表 GBK 编码)

Java FileInputStream默认编码方式

test2.txt编码方式为UTF-8

Java FileInputStream默认编码方式

运行结果输出为:

Java FileInputStream默认编码方式

Java的FileInputStream默认的编码方式就是文件的编码方式。

另外,如下代码:

InputStream is = new FileInputStream(new File(“C:\\Users\\Administrator\\Desktop\\test1.txt”));
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(new File(“C:\\Users\\Administrator\\Desktop\\test11.txt”)));
        byte[] bs = new byte[1024];
        int len = 0;
        while ((len = bis.read(bs)) != -1) {

            bos.write(bs, 0, len);
        }

这里读取test1.txt的数据直接写入到test11.txt,中间没有做任何编码转换,所以写完之后test1.txt文件的编码就是test1.txt的编码格式。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/138908.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号