last day (15 days later) » 

10:19 PM
0
Q: Java How to write a string in file as bytes and get string back

Maxim GotovchitsI need to write a string in a file as bytes in UTF-8 and then get these bytes back from file and convert it back to string and as a consiquence get the same string. May be it sounds easy but there is a hidden problem such as incorrect symbols in file. I mean that after appending in file it must c...

 
Why not use PrintWriter and BufferedReader as the file will always be bytes in the end anyway. They will be simpler, and possibly faster. Normally .txt means text not binary. BTW DataInput/OuputStream as a writeUTF/readUTF method which does something like this more efficiently.
 
@PeterLawrey As I mentioned I've tried many ways including PrintWriter, it needs to use toString() method for my operations. So then I will loose the ability to work with bytes correctly.
 
Not sure why you can't do that with Reader/Writer. From your description it appears your problem is that you are using binary and not text. but if you need a binary format you can use data streams.
 
@PeterLawrey Could you write some code? May be there's a solution that I don't unserstand...
 
A specific example would depend on your requirements, for example, I wouldn't write the length in a text file, only in a binary file. Writing the length makes editing the file unreasonably harder to edit. (With a binary format editing the file is not an option) It's also not clear to me if you want text or binary. BTW You should always print out an exception as there could be many causes.
Another thing which has me confused is; why would you toString() a String.
 
10:19 PM
@PeterLawrey Ok, as input I have 2 strings K and V, I must print K and V in file but as a sequence of bytes like d18ed187. And to get my string V by K from bytes from file I need to convert it back. But why do I need it if I have a HashMap where my string are displayed correctly? Yes, it's true but what if I turn off my programm and turn it on again? In this case my HashMap will be clear but "myfile.txt" will contain all the previous data! So to get this data I need to convert that from bytes.
 
hi
Did you see my examples for writing as text or as binary?
 
Yes
I'm reading it now
Give a minute to test that please
 
Have you considered using a Properties file. It has a load() and save() method.
 
I started programming Java 1 week ago so I'm pretty new at it so I have a lot if questions )
Yes
 
ok
Files are made of bytes in all languages btw ;)
 
10:24 PM
Actually I use System.getProperty("db.file")
oh
it a mistake
I didn't use load() and save()
But how can it help me?
BTW I need to rewrite my file each starting of program
 
That's what the examples do.
 
DataOutputStream dos = new BufferedOutputStream(new FileOutputStream("filename.dat")));
Here is a mistake
It requires DataOutputStream instead of BufferOutputStream
Oh no...
The same unnderstandable output
Look at that
somekey sval
but befor "somekey" is a big dot
and a lot of space
empty space
 

last day (15 days later) »