简体   繁体   中英

Java: String Comparison between FileOutputStream and ByteArrayOutputStream

I have a piece of code that writes to a file using a DataOutputStream wrapped around a FileOutputStream , and writes to a string using a DataOutputStream wrapped around a ByteArrayOutputStream , both using the writeDouble() method.

I then try to read from the file and compare what is read from the file, to the string,( BufferedReader wrapped around a StringReader wrapped around b(ByteArrayOutputStream).toString(), and BufferedReader wrapped around a FileReader ) and get the same result.

Even though

System.out.println(StrRead1.toString()) 

and

System.out.println(StrRead2.toString()) 

print the same thing in the terminal, when I compare the two using

(StrRead1.toString().equals(StrRead2.toString()))

they are never equal.(Str1 and Str2 being StringBuilder s)

Can somebody give me a hand? Thanks.

The result of writeDouble() is binary. You have no business trying to turn the result into a String, or read that data with a Reader. Use an InputStream and compare the bytes. Or use a DataInputStream and compare the result of readDouble() in each case.

But I really have no idea why you're doing this. writeDouble() is writeDouble(). It does the same thing, regardless of what output stream it's connected to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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