【问题标题】:Writing byte array to txt-file and reading it back将字节数组写入 txt 文件并读回
【发布时间】:2013-07-08 19:27:12
【问题描述】:

我有一个字节数组,需要写入 txt 文件。之后我需要从那里读取那个字节数组,这里出现了一个问题。我读了这个Convert Java string to byte array 但它只适用于正数。 这是我所拥有的

public static void main(String args[]) throws UnsupportedEncodingException
{
    byte [] a= new byte [2];
    a[0]=15;
    a[1]=-2;        
    String line = new  String(a, "UTF-8");      
    byte[] b = line.getBytes();
    System.out.println(b[0]);
    System.out.println(b[1]);
}

结果是

15
63

toString() 也不能正常工作。

提前感谢您的帮助。

【问题讨论】:

  • 不确定(还),但对我来说突出的一件事是您的println 期望int 作为输入,而不是byte。这可能是相关的。

标签: java string encoding bytearray


【解决方案1】:

对我来说,这保留了负值:

package com.sandbox;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;

public class Sandbox {

    public static void main(String args[]) throws UnsupportedEncodingException {
        byte[] a = new byte[2];
        a[0] = 15;
        a[1] = -2;
        String line = new String(a);
        byte[] b = line.getBytes();
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
        System.out.println(Charset.defaultCharset());
    }        
}

对我来说,这个输出:

[15, -2]
[15, -2]
windows-1250

基本上,您使用了错误的字符集来保留负字节。 You can find more info here. 实际上,您尝试做的事情的缺陷是首先将字节放入字符串中。 As you can see here,

...如果您以 byte[] 开头并且它实际上不包含文本数据,则没有“正确转换”。字符串用于文本,byte[] 用于二进制数据,唯一真正明智的做法是避免在它们之间进行转换,除非万不得已。

如果你真的必须使用 String 来保存二进制数据,那么最安全的方法是使用 Base64 编码。

如果您实际上不需要将其存储在字符串中,则应将输入/输出流写入/读取到文件中。有很多 SO 答案向您展示如何做到这一点。

【讨论】:

    【解决方案2】:

    我编写了我的小函数,将[-3, 123, 89, 0, -34, 78, -45, -78] 之类的字符串转换为字节数组。我不知道如何使用 Base64 编码。所以我写了我的小函数。 要得到上面提到的字符串,我们应该做String line = new String(Arrays.toString(name_of_array))

    public class StringToByteArray {
    public static void main(String args[])
    {
        String line= "[-3, 123, 89, 0, -34, 78, -45, -78]";
        String some=line.substring(1, line.length()-1);     
        int element_counter=1;
    
        for(int i=0; i<some.length(); i++)
        {           
            if (some.substring(i, i+1).equals(","))
            {
                element_counter++;
            }       
    
        }
        int [] comas =new int[element_counter-1];
        byte [] a=new byte[element_counter];
        if (a.length==1)
        {
            a[0]= Byte.parseByte(some.substring(0));
        }       
        else 
        {
            int j=0;
            for (int i = 0; i < some.length(); i++) 
            {
                if (some.substring(i, i+1).equals(","))
                {
                    comas[j]=i;
                    j++;
                }
            }           
            for (int i=0; i<element_counter; i++)
            {
                if(i==0)
                {
                    a[i]=Byte.parseByte(some.substring(0, comas[i]));
                }
                else if (i==element_counter-1)
                {
                    a[i]=Byte.parseByte(some.substring(comas[comas.length-1]+2));
                }
                else
                {
                    a[i]=Byte.parseByte(some.substring(comas[i-1]+2, comas[i]));
                }
    
            }
        }
        System.out.println(a[0]);
    
    }
    }
    

    结果-3

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 2016-09-01
      • 2023-04-01
      • 2018-08-09
      • 1970-01-01
      • 2014-04-10
      • 2011-08-18
      相关资源
      最近更新 更多