[英]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. 我参与了一个项目,我们通过修改指定位置的字节来隐藏mp3文件中的信息。 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.
我发现在线代码可以让我写和读字节,我正在用它来读取mp3文件中的前10个字节。
However there's a problem, it goes up until the 4th byte, after that the program ends. 但是有一个问题,它会一直持续到第4个字节,然后程序结束。 Or in other words, in my for cycle it only goes until i=4.
换句话说,在我的循环中它只会持续到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. 如果你注意到程序应该以system.out消息结束,那么循环会以某种方式结束那里,该消息将“程序结束”但不会出现。 The code's below.
代码如下。 I've tried with several mp3 files and the results the same.
我试过几个mp3文件,结果相同。
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'
). 问题在于IDE处理
'\\0'
字符的方式(第4个字节是'\\0'
)。 In order to see the real output change the print statement (inside the loop) to: 为了看到实际输出,将print语句(在循环内)更改为:
System.out.println("Read " + i + "th character of file: " + ch);
(that is: omit char (char)
casting). (即:省略char
(char)
cast)。 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: 除此之外,我建议如下:
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: 实际上该程序存在一些问题:
byte
to a char
is dodgy. byte
转换为char
是狡猾的。 Clearly some of the bytes don't correspond to printable characters. 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.
如果您不确定自己在做什么,我建议不要使用IDE作为初学者。 Rather, use the command line.
而是使用命令行。
Put Edit.java
and a.mp3
in the same folder. 将
Edit.java
和a.mp3
放在同一个文件夹中。 Then, type the following: 然后,键入以下内容:
javac Edit.java
java Edit
This should produce the correct output. 这应该产生正确的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.