I have a file in visual studio with the following contents:"{"Name":"Pete"}" If I read the file with the following code it appears to create a string with the original value:
byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
string jsonResponse = System.Text.Encoding.UTF8.GetString(byteArray);
However, the string is actually different to the version that exists if I use the following code:
string jsonResponse = "{\"Name\":\"Pete\"}";
Why? (The reason I think it is different is because when I pass each version to a json deserializer it behaves differently)
Thanks.
Since bytes is the binary data while String is character data. It is important to know the original encoding of the text from which the byte array has created. When we use a different character encoding, we do not get the original string back.
We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument.
The read(byte[ ], int, int) method of ByteArrayInputStream class in Java is used to read the given number of bytes into the given byte array from the ByteArrayOutputStream. This method is different from the above read() method as it can read several bytes at a time.
Byte arrays mostly contain binary data such as an image. If the byte array that you are trying to convert to String contains binary data, then none of the text encodings (UTF_8 etc.) will work.
Java Read File to Byte Array 1. Files.readAllBytes () – Java 8. Files.readAllBytes () is best method if you are using Java 7, 8 and above. 2. FileInputStream – Java 6. Use java.io.FileInputStream for reading the content of a file in Java 6. 3. FileUtils, IOUtils – Apache Commons IO. Another good ...
Since bytes is the binary data while String is character data. It is important to know the original encoding of the text from which the byte array has created. When we use a different character encoding, we do not get the original string back. Suppose, we have to read byte array from a file which is encoded in " ISO_8859_1 ".
So you cannot convert a large file into a single-byte array, though you can read large data using input stream, you need to process them in chunks or using multiple byte arrays. If you like this article and want to learn more about improved file IO in recent Java version, please check the following tutorials:
Files.readAllBytes () – Java 8 Files.readAllBytes () is best method if you are using Java 7, 8 and above. 2. FileInputStream – Java 6
Given your final comment in the question, I suspect the problem is that you've got a byte-order mark at the start of the file. Try loading the file like this instead:
string jsonResponse = File.ReadAllText(filePath);
I believe that will strip the BOM for you. Alternatively, you could try explicitly trimming it yourself:
jsonResponse = jsonResponse.TrimStart('\feff');
My guess would be that you have a terminating newline in your file.
You can easily verify if two strings have the same content in C# by just comparing them with a == b
.
Here's a short code sample that might help you identify the problem. The strings are output surrounded by < >
, which should help you identify surrounding whitespace (which, by the way, can be removed using String.Trim).
byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
string fromFile = System.Text.Encoding.UTF8.GetString(byteArray);
string fromString = "{\"Name\":\"Pete\"}";
if (fromFile == fromString) {
Console.WriteLine("Strings are the same.");
} else {
Console.WriteLine("Strings are different!");
Console.WriteLine("fromFile: <" + fromFile + ">");
Console.WriteLine("fromString: <" + fromString + ">");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With