All I want to do is read characters from a notepad and convert it to bytes and save it another file.
“a.txt” has the simple text in notepad “Hello World!”
However, in “b.txt”, I still see the human readable characters instead of the byte values. I also noticed that when I do a System.out.print(ba), it prints the bytes.
Can anyone tell me why Java does not write the byte values to “b.txt”?
import java.io.*;
class a {
static int f;
static String s;
public static void main(String args[])
throws IOException {
BufferedReader br = new BufferedReader(new FileReader ("a.txt"));
BufferedOutputStream w = new BufferedOutputStream(new FileOutputStream("b.txt"));
byte ba[] = new byte[1024];
while((s=br.readLine())!=null) {
ba = s.getBytes();
System.out.print(ba);
w.write(ba);
}
w.flush();
w.close();
br.close();
}
}
You’re writing raw byte values to the file.
Notepad reads them as ASCII text.
If you open either file in a hex editor, you will see the actual byte values.
If you want to see byte values in Notepad, you’ll need to print each byte to the file as a string, typically separated by spaces so that you can see each one.