简体   繁体   English

如何将字符串转换回字节以便写入Java文件?

[英]How to convert string back to bytes in order to write to file in Java?

I have a text file that contains the word "cool" in it. 我有一个文本文件,其中包含单词“ cool”。 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);

Output: "cool" turned into bytes 输出: “酷”变成字节

99111111108

Unfortunately the code below does not write "cool" back to the file, instead it writes 99111111108. 不幸的是,下面的代码没有将“ cool”写回到文件中,而是写入了99111111108。

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

Cannot be done. 无法完成。 The problem is that in the string "99111111108" the demarcations of where a byte starts and ends are not included. 问题是,在字符串“ 99111111108”中,不包括字节的开始和结束位置。 In other words, in this string is "9" the first character, or "99"? 换句话说,在此字符串中,第一个字符是“ 9”还是“ 99”?

If you had the byte values represented as strings, you can convert them back to bytes with the method Byte.valueOf. 如果您将字节值表示为字符串,则可以使用Byte.valueOf方法将它们转换回字节。

byte b = getByte();
String byteAsString = Byte.toString(b); 
System.out.println(byteAsString); //might print something like '111'

byte o = Byte.valueOf(byteAsString);
assertEquals(b,o); //true

To me though it seems like it would be better to just read the data into a String directly with 在我看来,最好直接将数据读取为String

new String(byteArray, encoding); 

like dnault suggested. 像dnault建议的那样。 With any string you get a character with 使用任何字符串,您都可以使用

"string".charAt(index);

Also it's possible in your loop to get a character directly by casting byte to char. 此外,还可以在循环中通过将字节转换为char直接获取字符。 You could do this 你可以这样做

for(byte b: data){
    x += (char)b;
}
System.out.println(x);

And this code would print "cool" given the example input you provided, although this is a very naive approach and you would run into trouble when using characters with values larger than byte's max value. 给定您提供的示例输入,此代码将显示“很酷”,尽管这是一种非常幼稚的方法,当使用字符值大于字节最大值的字符时,您会遇到麻烦。

Check this out: https://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html#toString(byte) 检查一下: https : //docs.oracle.com/javase/7/docs/api/java/lang/Byte.html#toString(byte)

toString toString

public static String toString(byte b) 公共静态字符串toString(字节b)

Returns a new String object representing the specified byte. 返回表示指定字节的新String对象。 The radix is assumed to be 10. 假定基数为10。

Parameters: b - the byte to be converted Returns: the string representation of the specified byte 参数:b-要转换的字节返回:指定字节的字符串表示形式

See Also: Integer.toString(int) 另请参见:Integer.toString(int)

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. 问题是,您要将字符串转换为字节,但随后又要将该字节转换为字符串/字符(解释为以10为底的数字-radix = 10),这意味着您实际上获得了每个字符串的ascii等效项字符(c = 99,o = 111,o = 111,l = 108),它是以10为底的数字。但是,您是每个数字的数字字符。 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. 当您将字符串转换回一个字节时,您得到的是数字字符的字节,而不是您想要的字母的字节。

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. 尚不清楚要转换为字节显示的内容,但是如果您确实想与位串(由0和1的数字字符组成的字符串)之间进行转换,则必须做更多的工作。

If you delimited the string you're building with some other character like a comma (eg 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. 如果用其他字符(例如,逗号)(例如99,111,111,108)来分隔正在构建的字符串,则可以假定分隔的子字符串是整数(对于常规ascii),并将它们传递给'Integer.parseInt(s)'或'Integer。 valueOf(s)',然后将其转换为char,然后将chars构建为字符串。

For example: 例如:

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();

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. 这里重要的一点是,至少对于Java,char只是整数,只是将char解释为ascii字符。

The basic dilemma is, I believe, that converting the bytes to strings is a destructive operation where the context is lost. 我认为,基本的难题是将字节转换为字符串是一种破坏性操作,会丢失上下文。 Ie 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). 字节是二进制数据,而字符串是一组字符(通常为ascii,但也包括各种UTF)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM