last day (15 days later) » 

6:35 PM
1
Q: Java problems while working with file

Maxim GotovchitsI've got some problems trying to delete from my string a subsequence \u000. Firstly, I read bytes [] from my file into string by String str = new String(bytes, "UTF8"); then I get the str which equals \u0004Word which means 4Word. 4 is length of word Word. So now I need to convert it to regular...

 
I see ` Words` with 1 space before the word.
 
'\u000' is an illegal Unicode escape. There have to be four digits after the \u, like '\u0000' or '\u0004'.
 
@Debasis updated
@DavidConrad yes, there's \u0004.
 
Unrelated, but you should use new String(data, StandardCharsets.UTF_8) instead to avoid the UnsupportedEncodingException which can't actually happen with UTF-8.
Can the string be over 127 characters long? If there was an extraneous character \u0080 or greater at the beginning of the string, it would cause problems interpreting the data as UTF-8. You need to remove the length before you convert it to a string.
 
@DavidConrad I get the same string =(
 
6:35 PM
Of course you get the same string, but the overload of the constructor that takes a Charset doesn't throw an UnsupportedEncodingException.
 
@DavidConrad I found out the problem. Question is updated.
@Debasis Question is updated
 
That's weird, you write the length out before the key or the value, and it disappears, but when you read the data back in, there are these weird extra bytes whose values happen to correspond exactly to the lengths of the strings.
 
It disappears when I convet it to string str and make println(str)
But these numbers are written in file
I have no idea what to do with it
may I should convert it somehow>
?
 
When you output it with println it is likely that your terminal just doesn't know how to display these control characters. That doesn't mean they aren't there.
Why don't you just use an ObjectOutputStream and write the entire HashMap out, and read it back in as a HashMap?
These numbers are written to the file because YOU ARE WRITING THEM TO THE FILE when you do stream.write((int)bytesKey.length);
 
Actually, I'm new at Java so I don't know what is ObjectOutputStream
Yes I agree
I must write data in file as bytes
and get back strings
 
6:39 PM
If you want to write a byte that indicates how many characters are in the string, followed by the characters, then you need to read it back the same way. Read one byte, use its value to tell you how many characters are after it, then read that many bytes, and convert it to a string.
The problem is you are writing the data out as length byte, bytes of string, length byte, bytes of string, etc. but you are reading it in a completely different way: read all the bytes, and convert them to one string.
 
hm
Yes, looks like you are right, but how can I do that?
Which functions must be used?
 
You use a DataOutputStream to write the data; use a DataInputStream to read the data.
 
Just I'm new at Java so =(
 
Look at the javadoc for DataInputStream, it has read methods to read single bytes or arrays of bytes.
You figured out how to use the DataOutputStream, right?
 
I hope so)
So if I read all bytes by DataOutputStream I will get what i need?
using the same convrting etc...
but instead of Path I will use Stream
right?
 
6:44 PM
read all bytes, but read the length byte first, then read that many bytes
 
Ok, give me couple minutes
What should I do to open file for writing in it?
DataInputStream stream = new DataInputStream(new FileInputStream(System.getProperty("db.file"), true)); doesn't work
 
That looks like it should work. What error did you get?
wait, what is that true parameter on the end?
oh, you only need that on FileOutputStream; remove that ", true"
 
File file = new File(System.getProperty("db.file"));
FileInputStream stream = new FileInputStream(System.getProperty("db.file"));
byte[] data = new byte[(int) file.length()];
stream.read(data);
That's what I done
But I have the same result
numbers are not displayed on console
Wait a sec, I wanna try it like you said without ,true
 
7:00 PM
You're still trying to read the entire file as one byte[], when you write it out as separate pieces: the length of a key, then the bytes of the key, then the length of a value, then the bytes of the value, then on to the next entry in the Map
 
Doenst work too=(
I will get back in 10 minutes
 
You need to read it the same way you wrote it. Read a length, then a key, then a length, then a value, then stick them in a map, and so on.
 
7:14 PM
I'm bacj
back
 

last day (15 days later) »