在java中覆盖文件的二进制值

发布于 2024-11-02 20:25:32 字数 1521 浏览 1 评论 0原文

我参与了一个项目,我们通过修改指定位置的字节来隐藏 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-11-09 20:25:32

我刚刚尝试了你的代码,它对我有用。问题在于您的 IDE 处理 '\0' 字符的方式(第 4 个字节是 '\0')。为了查看真实的输出,将 print 语句(在循环内)更改为:(

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

即:省略 char (char) 转换)。然后您将得到以下输出:

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

除此之外,我建议如下:

  • 考虑使用预制库来检索/写入 MP3 元数据。这比从头开始实现这个逻辑要好得多。
  • 您不需要在循环内搜寻()。当您打开文件时,您位于位置 0,并且每个 readByte() 都会将位置前进 1。
  • 如果将 ch 变量的定义移到循环内,代码将更具可读性。如果您只在循环内部使用它,则没有理由在外部定义它。

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.
筱果果 2024-11-09 20:25:32

我已经运行了你的程序并且运行良好。如果您不太确定自己在做什么,我建议初学者不要使用 IDE。相反,使用命令行。

Edit.javaa.mp3 放在同一文件夹中。然后,键入以下内容:

javac Edit.java
java Edit

这应该会产生正确的输出。

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.

感悟人生的甜 2024-11-09 20:25:32

我看不出代码有任何问题,而且我不明白它如何产生您报告的输出。

我怀疑您正在运行的类文件与您向我们展示的源代码不匹配。


实际上,该程序有一些问题:

  1. 您应该在finally 子句中关闭RAF。
  2. 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:

  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 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文