I’m involved in a project where we hide information in an mp3 file by modifying bytes at a specified position. I found code online which lets me write and read bytes and I was playing with it to read the first 10 bytes in a mp3 file.
However there’s a problem, it goes up until the 4th byte, after that the program ends. Or in other words, in my for cycle it only goes until i=4.
This is the output I get.
Read 0th character of file: I
Read 1th character of file: D
Read 2th character of file: 3
Read 3th character of file:
Read 4th character of file:
Process completed.
The cycle somehow ends there, if you notice the program should end with the system.out message that goes “end of program” but not even that comes out. The code’s below. I’ve tried with several mp3 files and the results the same.
What could be the problem? why does my program ends without even giving me an error message?
import java.io.File; import java.io.RandomAccessFile; import java.io.IOException;public class Edit {
private static void doAccess() { try { //Reads mp3 file named a.mp3 File file = new File("a.mp3"); RandomAccessFile raf = new RandomAccessFile(file, "rw"); //In this part I try to read the first 10 bytes in the file byte ch; for (long i = 0; i < 10 ; i++) { raf.seek(i); //position ourselves at position i ch = raf.readByte(); //read the byte at position i System.out.println("Read "+ i + "th character of file: " + (char)ch); //print the byte at that position //repeat till position 10 } System.out.println("End of program"); raf.close(); } catch (IOException e) { System.out.println("IOException:"); e.printStackTrace(); } } public static void main(String[] args) { doAccess(); } }
Thanks in advance!
I just tried your code and it works for me. The problem is with the way your IDE handles
'\0'characters (the 4th byte is'\0'). In order to see the real output change the print statement (inside the loop) to:(that is: omit char
(char)casting). You will then get this output:Other than that I suggest the following: