如何将字符串转换回字节以便写入Java文件? [英] How to convert string back to bytes in order to write to file in Java?

查看:56
本文介绍了如何将字符串转换回字节以便写入Java文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中包含单词"cool".我读取了此文件中的所有字节,并将其转换为字符串.但是,在另一个试图将相同字符串转换为字节以写入文件的函数中,我没有得到期望的结果.

I have a text file that contains the word "cool" in it. I read all the bytes in this file and turn it into a string. However, In another function where I am trying to turn the same string back to bytes to write into the file I don't get what I expected.

Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);

String x = new String();
for(byte b: data){
    x += Byte.toString(b);
}  
System.out.println(x);

输出:酷"变成了字节

99111111108

不幸的是,下面的代码没有将"cool"写回到文件中,而是写入了99111111108.

Unfortunately the code below does not write "cool" back to the file, instead it writes 99111111108.

str = "99111111108";
FileOutputStream C = new FileOutputStream("new.txt");
C.write(str.getBytes());
C.close();   

推荐答案

查看以下内容:

toString

toString

公共静态字符串toString(字节b)

public static String toString(byte b)

返回一个表示指定字节的新String对象.基数假定为10.

Returns a new String object representing the specified byte. The radix is assumed to be 10.

参数:b-要转换的字节返回:字符串指定字节的表示形式

Parameters: b - the byte to be converted Returns: the string representation of the specified byte

另请参阅:Integer.toString(int)

See Also: Integer.toString(int)

问题是您要将字符串转换为字节,但是随后然后将该字节转换为字符串/字符(解释为以10为底的数字-基数= 10),这意味着您实质上可以获得每个字符的ascii等效项(c = 99,o = 111,o = 111,l = 108),是以10为底的数字.但是,您是每个数字的数字字符.当您将字符串转换回一个字节时,您得到的是数字字符的字节,而不是您想要的字母的字节.

The issue is that you're turning the string into bytes, but then you're going and turning that byte into a string/character (interpreted as a base 10 number - radix=10) which means you essentially get the ascii equivalent of each character (c=99, o=111, o=111, l=108) which is a number in base 10. However you're the numeric character for each digit. When you go to turn the string back into a byte you're getting the byte for the numeric character not the byte for the letter like you want.

根据实际情况,您将需要找到其他方法.目前尚不清楚您试图通过转换为字节来显示什么,但是如果您确实想在位串(由0和1的数字字符组成的字符串)之间来回转换,则必须做更多的工作.

Depending on what you're actually after, you're going to need to find a different approach. It's not clear what you are trying to show by converting to bytes, but if you really want to convert to and from a bitstring (a string composed of the numeric characters for 0s and 1s) you'll have to do more work.

如果您用其他字符(例如,逗号)(例如99,111,111,108)分隔正在构建的字符串,则可以假定分隔的子字符串为整数(对于常规ascii),并将其传递给'Integer.parseInt(s)'或'Integer.valueOf(s)',然后转换为char,然后将chars构建为字符串.

If you delimited the string you're building with some other character like a comma (e.g. 99,111,111,108) then you could assume the delimited substrings were integers (for regular ascii) and pass them to 'Integer.parseInt(s)' or 'Integer.valueOf(s)' and then do a conversion to char and then build the chars into a string.

例如:

StringBuilder sb = new StringBuilder();

String str = "99,111,111,108"; // result of initial conversion
String[] sa = str.split(",");

char ch = '';

for(String s : sa) {
   ch = Integer.parseInt(s);
   sb.append(ch);
}

FileOutputStream fos = new FileOutputStream("new.txt");

fos.write(sb.toString().getBytes());
fos.close();

这里重要的一点是,至少对于Java而言,char只是整数,只是将char解释为ascii字符.

An important note here is that, for Java at least, chars are just integers except that they a char is interpreted as being an ascii character.

我认为,基本的难题是将字节转换为字符串是丢失上下文的破坏性操作.IE.计算机不再知道有关原始字节的任何信息,而仅知道新生成的字符串是什么.字节是二进制数据,而字符串是一组字符(通常为ascii,但也包括各种UTF).

The basic dilemma is, I believe, that converting the bytes to strings is a destructive operation where the context is lost. I.e. the computer no longer knows anything about the original bytes, only what the newly generated string is. Bytes are binary data, but strings are a group of characters (generally ascii, but also UTF of various kinds).

这篇关于如何将字符串转换回字节以便写入Java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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