首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将byte[]写入文件并从文件读取byte[]

将byte[]写入文件并从文件读取byte[]
EN

Stack Overflow用户
提问于 2015-12-30 02:00:13
回答 2查看 647关注 0票数 0

我正在编写一个byte[]来归档和读取它。但是为什么写和读的byte[]是不同的呢?下面是我的代码:

代码语言:javascript
运行
复制
import java.io.*;

public class test{
    public static void main(String argv[]){
        try{
            byte[] Write = "1234567812345678".getBytes();
            byte[] Read = new byte[16];

            File test_file = new File("test_file");
            test_file.mkdir();

            String path = new String(test_file.getAbsolutePath()+"\\"+"test.txt");
            File test_out = new File(path);
            test_out.createNewFile();

            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(test_out));
            out.write(Write);
            out.close();

            File test_in = new File(path);
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(test_in));
            while(in.available()>0)
                in.read(Read);
            in.close();

            System.out.println(Write);
            System.out.println(Read);
        }catch ( Exception e ){
            e.printStackTrace();
        }
    }
}

这是输出;输入和输出是不同的:

代码语言:javascript
运行
复制
[B@139a55 
[B@1db9742
EN

Stack Overflow用户

发布于 2015-12-30 02:02:08

[B@139a55

[B@1db9742

这些是打印byte[]的输出--它是对象的哈希码。它与它的实际内容无关。

它只告诉您正在打印两个不同的对象-它们的内容可能仍然相同。

您应该打印字节数组的实际内容:What's the simplest way to print a Java array?

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34516419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档