简体   繁体   English

将ASCII字符写入Java / Android中的文件时出错

[英]Error in writing ascii characters into a file in java/android

I have the following code: 我有以下代码:

private char[] headerToWrite;
protected String workingFileName;

private void writeHeaderToFile()
{
    try
    {
        String completeFile = new String(headerToWrite);

        File myFile = new File(workingFileName);

        FileOutputStream fOut = new FileOutputStream(myFile);

        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

        myOutWriter.append(completeFile);
        myOutWriter.close();
    }
    catch (IOException ioe) 
    {
        ioe.printStackTrace();
    }
}

In the above code, the variable headerToWrite contains an array, where the first few values are: [1, Q, H, S, , 4, ±, Q, .....] . 在上面的代码中,变量headerToWrite包含一个数组,其中前几个值是: [1, Q, H, S, , 4, ±, Q, .....] This in hex is [31, 51, 48, 53, 01, 34, B1, 51...] . 十六进制为[31, 51, 48, 53, 01, 34, B1, 51...]

It is used to create a string completeFile which = 1QHS 4±Q... 它用于创建一个字符串completeFile = 1QHS 4±Q ...

However, when the file is being written, the file contains 1QHS 4űQ..... which in hex is [31 51 48 53 01 34 c2 b1 51] .... 但是,在写入文件时,文件包含1QHS4űQ .....,其十六进制为[31 51 48 53 01 34 c2 b1 51] ...。

I couldn't understand why there was an additional c2 but I discovered that the bytes inside myOutWriter were as follows: [49, 81, 72, 83, 1, 52, -62, -79, 81] .... 我不明白为什么还要增加一个c2,但是我发现myOutWriter中的字节如下: [49, 81, 72, 83, 1, 52, -62, -79, 81] 49,81,72,83,1,52 [49, 81, 72, 83, 1, 52, -62, -79, 81] ...。

The interesting point here is the -62, -79 which seems to be responsible for c2, b1 . 有趣的是, -62, -79似乎与c2, b1 For it to work, -62, -79 should just be 177 , which is the decimal for b1. 为了使其正常工作, -62, -79应该仅为177 ,这是b1的小数。 Interestingly 177 + 79 is 256. 有趣的是177 + 79是256。

So evidently, in the transfer from ascii characters in completeFile to bytes in myOutWriter , c2 is being added. 因此很明显,在从completeFile中的 ascii字符到myOutWriter中的字节的传输中 ,添加了c2

I was wondering if anyone could explain why and how to fix it. 我想知道是否有人可以解释为什么以及如何解决它。

Thanks 谢谢

it could be because by appending a String, it actually converts it to byte array using default encoding, which is probably UTF-8. 这可能是因为通过附加字符串,它实际上使用默认编码将其转换为字节数组,该编码可能是UTF-8。 To fix this, convert your string into byte array by using ISO 8859-1 encoding and write that byte array to your output stream. 要解决此问题,请使用ISO 8859-1编码将字符串转换为字节数组,然后将该字节数组写入输出流。

The -79 can be explained by overflow. -79可以用溢出来解释。 As byte is a signed char and 177 > 127 (which is the maximal value for a byte), it will overflow and -79 is the result. 由于byte是带符号的char并且177> 127(这是一个字节的最大值),它将溢出并且结果为-79。 I can not really explain the -62, but you are clearly using the wrong encoding (probably UTF-8). 我无法真正解释-62,但是您显然使用了错误的编码(可能是UTF-8)。 Try using a different encoding ( ISO 8859-1 might do the trick). 尝试使用其他编码( ISO 8859-1可以解决问题)。

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

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