开发者

Overwriting binary value of file in java

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 (l开发者_StackOverflow中文版ong 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:

System.out.println("Read " + i + "th character of file: " + ch);

(that is: omit char (char) casting). You will then get this output:

Read 0th character of file: 73
Read 1th character of file: 68
Read 2th character of file: 51
Read 3th character of file: 3
Read 4th character of file: 0
Read 5th character of file: 0
Read 6th character of file: 0
Read 7th character of file: 0
Read 8th character of file: 15
Read 9th character of file: 118
End of program

Other than that I suggest the following:

  • Consider using a pre-made library for retriving/writing MP3 meta-data. This is far better than implementing this logic from scratch.
  • You don't need to seek() inside the loop. when you open the file you're at position 0, and every readByte() advances the position by 1.
  • The code will be more readable if you move the definition of the ch variable inside the loop. If you only use it inside the loop there's no reason to define it outside.


I've ran your program and it works fine. I'd suggest not using an IDE for starters if you're not really sure what you're doing. Rather, use the command line.

Put Edit.java and a.mp3 in the same folder. Then, type the following:

javac Edit.java
java Edit

This should produce the correct output.


I cannot see anything wrong with the code, and I don't understand how it could possibly produce the output that you report.

I suspect that you are running a class file that does not match the source code you showed us.


Actually there are a couple of things wrong with the program:

  1. You should close the RAF in a finally clause.
  2. Casting a byte to a char is dodgy. Clearly some of the bytes don't correspond to printable characters. I guess it is possible that one of them is some character that JCreator interprets as an end-of-file character ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜