在java中覆盖文件的二进制值
我参与了一个项目,我们通过修改指定位置的字节来隐藏 mp3 文件中的信息。我在网上找到了可以写入和读取字节的代码,我正在使用它来读取 mp3 文件中的前 10 个字节。
然而有一个问题,它一直上升到第 4 个字节,之后程序结束。或者换句话说,在我的 for 循环中,它只会持续到 i=4 为止。
这是我得到的输出。
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.
如果您注意到程序应该以“程序结束”的 system.out 消息结束,但甚至没有出现,则循环以某种方式结束。代码如下。我尝试过几个 mp3 文件,结果相同。
可能是什么问题?为什么我的程序结束时甚至没有给我一个错误消息?
import java.io.File; import java.io.RandomAccessFile; import java.io.IOException;公开课编辑{
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(); } }
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚尝试了你的代码,它对我有用。问题在于您的 IDE 处理
'\0'
字符的方式(第 4 个字节是'\0'
)。为了查看真实的输出,将 print 语句(在循环内)更改为:(即:省略 char
(char)
转换)。然后您将得到以下输出:除此之外,我建议如下:
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:
我已经运行了你的程序并且运行良好。如果您不太确定自己在做什么,我建议初学者不要使用 IDE。相反,使用命令行。
将
Edit.java
和a.mp3
放在同一文件夹中。然后,键入以下内容:这应该会产生正确的输出。
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
anda.mp3
in the same folder. Then, type the following:This should produce the correct output.
我看不出代码有任何问题,而且我不明白它如何产生您报告的输出。
我怀疑您正在运行的类文件与您向我们展示的源代码不匹配。
实际上,该程序有一些问题:
byte
转换为char
是不可靠的。显然,某些字节与可打印字符不对应。我猜想其中之一可能是 JCreator 解释为文件结束字符的某个字符......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 achar
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 ...