string - Java problems while working with file -
i've got problems trying delete string subsequence \u000
.
firstly, read bytes [] file string string str = new string(bytes, "utf8");
str
equals \u0004word
means 4word
. 4
length of word word
. need convert regular 4words
. replaceall("\u000", "");
, replaceall("\\\\u000", "")
etc doesn't work. how that?
void fillingstorage() throws exception{ path path = paths.get(system.getproperty("db.file"));//that's file byte[] info = files.readallbytes(path); string str = new string(data, "utf8"); system.out.println(str); string res = str.replaceall("i don't know write here cos nil i've tried works"); return; }
update! firstly, fill hashmap key -> value , key1 -> value1
. write in file bytes. when seek convert string , print see: key value key1 value1
instead of 3key 5value 4key1 6value1
. suprisingly if @ string print see smth that: \u0003key \u0005value etc...
looks string contains these numbers java can't print them.
this how write bytes in file:
dataoutputstream stream = new dataoutputstream(new fileoutputstream(system.getproperty("db.file"), true)); (map.entry<string, string> entry : storage.entryset()) { byte[] byteskey = entry.getkey().getbytes(standardcharsets.utf_8); stream.write((int)byteskey.length);//it disappears! stream.write(byteskey); byte[] bytesval = entry.getvalue().getbytes(standardcharsets.utf_8); stream.write((integer)bytesval.length);//disappears too! stream.write(bytesval); } stream.close();
first of all, requirement not phone call regular expressions, should have used replace()
instead.
second, \uxxxx
character literal syntax in java, not clear have characters \
u
0
0
0
in string; much more logical byte array starts single byte equal 4, string length.
in case should discard initial byte array when converting string
, using constructor accepts offset
, len
arguments.
if happen indeed have chars in string, 1 time again using substring
rid of initial 6 characters should need.
java string replace replaceall
No comments:
Post a Comment