I try to read the byte from "bytefile", changing it to String and store it to "stringfile". The code below is how I perform.
PrintWriter writer = new PrintWriter(new FileOutputStream(new File("stringfile"), true));
RandomAccessFile file = new RandomAccessFile("bytefile", "r");
byte[] b = new byte[(int)file.length()];
file.readFully(b);
String str = new String(b, StandardCharsets.UTF_8);
writer.write(str);
writer.close();
Then I try to convert the string in "stringfile" to byte and store in "newbytefile". However, the outcome do not meet my expectation.
String charset = "UTF-8";
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("stringfile"), charset));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream("newbytefile"), charset));
String line;
while ((line = reader.readLine()) != null){
writer.println(line.getBytes());
}
reader.close();
writer.close();
The pattern in "bytefile" is something like this:
<9e>zgóoG^P$@0<81>^B*É^X¿uú<9b>^@
The pattern in "stringfile" is something like this:
�zg�oG^P$@0�^B*�^X�u��^@�� V�
However, the pattern in "newbytefile" is something like this:
[B@40226788
How can I covert the string in "stringfile" to byte that is same to the original patten in "bytefile"?