I need to add header info to a JPEG file in order to get it to work properly when shared on some websites, I've tracked down the correct info through a lot of Hex digging, but now I'm kind of stuck trying to get it into the file. I know where in the file it needs to go, and I know how long it is, my problem is that RandomAccessFile just overwrites existing data in the file and FileOutputStream appends the data to the end. I don't want either, I want to INSERT data starting at the third byte.
我需要将标题信息添加到JPEG文件中,以便在某些网站上共享时能够正常工作,我通过大量的Hex挖掘跟踪了正确的信息,但现在我有点陷入困境它进入文件。我知道文件需要去哪里,我知道它有多长,我的问题是RandomAccessFile只是覆盖文件中的现有数据而FileOutputStream将数据附加到最后。我也不想要,我想从第三个字节开始插入数据。
My example code:
我的示例代码:
File fileToChange = new File("someimage.jpg");
byte[] i = new byte[2];
i[0] = (byte)Integer.decode("0xcc");
i[1] = (byte)Integer.decode("0xcc");
RandomAccessFile f =
new RandomAccessFile(new File("videothing.jpg"), "rw");
long aPositionWhereIWantToGo = 2;
f.seek(aPositionWhereIWantToGo); // this basically reads n bytes in the file
f.write((byte[])i);
f.close();
So this doesn't work because it overwrites, and does not insert, I can't find any way to just insert data into a file
所以这不起作用,因为它覆盖,并且不插入,我找不到任何方法只是将数据插入文件
2 个解决方案
#1
0
You will need to use OutputSteam and InputStream in tandem. Read the file using InputStream and write first 2 bytes from the original file. The insert your hexadecimal content and then read rest of the input stream and write to the OutputSteam.
您将需要串联使用OutputSteam和InputStream。使用InputStream读取文件并从原始文件中写入前2个字节。插入十六进制内容,然后读取输入流的其余部分并写入OutputSteam。
Method to be used for reading:
用于阅读的方法:
public int read(byte b[], int off, int len)
Method to be used for writing:
用于写作的方法:
public void write(byte b[], int off, int len)
#2
3
Rewrite a copy of the file inserting the data into it at the desired place. Or write an OutputStream
that injects the data if you want to do it on-the-fly while transmitting, for example, an HTTP response.
重写在所需位置将数据插入其中的文件副本。或者编写一个OutputStream,如果要在传输时动态执行,例如HTTP响应,则会注入数据。
#1
0
You will need to use OutputSteam and InputStream in tandem. Read the file using InputStream and write first 2 bytes from the original file. The insert your hexadecimal content and then read rest of the input stream and write to the OutputSteam.
您将需要串联使用OutputSteam和InputStream。使用InputStream读取文件并从原始文件中写入前2个字节。插入十六进制内容,然后读取输入流的其余部分并写入OutputSteam。
Method to be used for reading:
用于阅读的方法:
public int read(byte b[], int off, int len)
Method to be used for writing:
用于写作的方法:
public void write(byte b[], int off, int len)
#2
3
Rewrite a copy of the file inserting the data into it at the desired place. Or write an OutputStream
that injects the data if you want to do it on-the-fly while transmitting, for example, an HTTP response.
重写在所需位置将数据插入其中的文件副本。或者编写一个OutputStream,如果要在传输时动态执行,例如HTTP响应,则会注入数据。