将字节数组写入txt文件并读回 [英] Writing byte array to txt-file and reading it back

查看:125
本文介绍了将字节数组写入txt文件并读回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组,我需要写入txt文件。之后,我需要从那里读取该字节数组,这里出现问题。我阅读了这个将Java字符串转换为字节数组
但它只能用正数。
这里我有什么

  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()也不行。



提前感谢您的帮助。

解决方案

将字符串像 [ - 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) ,))
{
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; (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



我希望有帮助


I have a byte array, that I need to write to txt-file. After that I need to read that byte array from there and here appears a problem. I read this Convert Java string to byte array But it works only with positive numbers. Here what I have

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]);
}

and result is

15
63

toString() doesn't work as well.

Thank you in advance for help.

解决方案

I wrote my small function that converts String like [-3, 123, 89, 0, -34, 78, -45, -78] to byte array. I didn't know how to use Base64 encoding. So I wrote my small function. To get Above mentioned string we should do 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]);

}
}

Result -3

I Hope it will be helpful

这篇关于将字节数组写入txt文件并读回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆