I'm writing arbitrary byte arrays (mock virus signatures of 32 bytes) into arbitrary files, and I need code to overwrite a specific file given an offset into the file. My specific question is: is there source code/libraries that I can use to perform this particular task?
我正在将任意字节数组(模拟32个字节的病毒签名)写入任意文件,我需要代码来覆盖给定文件偏移量的特定文件。我的具体问题是:是否有可用于执行此特定任务的源代码/库?
I've had this problem with Python file manipulation as well. I'm looking for a set of functions that can kill a line, cut/copy/paste, etc. My assumptions are that these are extremely common tasks, and I couldn't find it in the Java API nor my google searches.
我也遇到过Python文件操作的问题。我正在寻找一组可以杀死一行,剪切/复制/粘贴等功能。我的假设是这些是非常常见的任务,我在Java API和谷歌搜索中都找不到它。
Sorry for not RTFM well; I haven't come across any information, and I've been looking for a while now.
很抱歉没有RTFM;我没有遇到任何信息,我一直在寻找一段时间。
5 个解决方案
#1
Maybe you are looking for something like the RandomAccessFile class in the standard Java JDK. It supports reads and writes at some offset, as well as byte arrays.
也许您正在寻找标准Java JDK中的RandomAccessFile类。它支持某些偏移量的读写,以及字节数组。
#2
Java's RandomAccessFile
is exactly what you want.
Java的RandomAccessFile正是你想要的。
It includes methods like seek(long)
that allow you to move wherever you need in the file. It also allows for reading and writing at the same time.
它包括seek(long)等方法,允许您在文件中的任何位置移动。它还允许同时进行读写。
#3
As far as I know, Java has primarily lower level functions for manipulating files directly. Here is the best I've come up with
据我所知,Java主要有较低级别的函数来直接操作文件。这是我提出的最好的
-
The actions you describe are standard in the Swing world, and for text comes down to manipulating a
Document
object. These act on data in memory. The classjava.nio.channels.FileChannel
has similar methods that act directly on a file. Neither fine the end of lines automatically, but other classes injava.io
andjava.nio
do.您描述的操作在Swing世界中是标准的,而文本则归结为操纵Document对象。它们对内存中的数据起作用。 java.nio.channels.FileChannel类具有直接作用于文件的类似方法。既没有自动结束行,也没有java.io和java.nio中的其他类。
-
Apache Commons has a sandbox library called Flatfile which looks like it does what you want. The problem is that no code has been released yet. You may, however, want to talk to people working on it to get some more ideas. I didn't do a general check on libraries.
Apache Commons有一个名为Flatfile的沙箱库,它看起来像你想要的那样。问题是尚未发布任何代码。但是,您可能希望与工作人员交谈,以获得更多想法。我没有对图书馆进行一般性检查。
#4
Have you looked into File/FileReader/FileWriter/BufferedReader? You can get the contents of the files and manipulate it as you like, you can search the data in the files, you can overwrite files, create new, append to an existing....
你看过File / FileReader / FileWriter / BufferedReader吗?您可以获取文件的内容并根据需要进行操作,您可以搜索文件中的数据,可以覆盖文件,创建新文件,附加到现有文件....
I am not sure this is exactly what you are asking for but I use these APIs all the time for logging, RTF editors, text file creation for email, and many other things.
我不确定这正是您所要求的,但我一直使用这些API进行日志记录,RTF编辑器,电子邮件的文本文件创建以及许多其他内容。
As far as cut/copy/past goes, I have not come across the ability to do that directly, however, you can output the contents of the file and "copy" what part of it you want and "paste" it into a new file, or append it to an existing.
至于剪切/复制/过去,我没有遇到直接这样做的能力,但是,你可以输出文件的内容并“复制”你想要的那部分并将其“粘贴”到一个新的文件,或将其附加到现有文件。
#5
While writing a byte array to a file is a common task, writing to a give file 32-bytes byte array just once is just not something you are going to find in java.io :)
将字节数组写入文件是一项常见的任务,只写一次给定文件32字节字节数组只是你在java.io中找不到的东西:)
To get started, would the below method and comments look reasonable to you? I bet someone here, maybe even myself, could whip it out quick like.
首先,以下方法和评论对您来说是否合理?我打赌有人在这里,甚至是我自己,可以快速地鞭打它。
public static void writeFauxVirusSignature(File file, byte[] bytes, long offset) {
//open file
//move to offset
//write bytes
//close file
}
Questions:
- How big could the potential target files be?
- Do you need performance?
潜在的目标文件有多大?
你需要表现吗?
I ask because clean, easy to read code would use Apache Commons lib's, but large file writes in a performance sensitive environment will necessitate using java.nio
libraries
我问,因为干净,易于阅读的代码将使用Apache Commons lib,但在性能敏感的环境中进行大文件写入将需要使用java.nio库
#1
Maybe you are looking for something like the RandomAccessFile class in the standard Java JDK. It supports reads and writes at some offset, as well as byte arrays.
也许您正在寻找标准Java JDK中的RandomAccessFile类。它支持某些偏移量的读写,以及字节数组。
#2
Java's RandomAccessFile
is exactly what you want.
Java的RandomAccessFile正是你想要的。
It includes methods like seek(long)
that allow you to move wherever you need in the file. It also allows for reading and writing at the same time.
它包括seek(long)等方法,允许您在文件中的任何位置移动。它还允许同时进行读写。
#3
As far as I know, Java has primarily lower level functions for manipulating files directly. Here is the best I've come up with
据我所知,Java主要有较低级别的函数来直接操作文件。这是我提出的最好的
-
The actions you describe are standard in the Swing world, and for text comes down to manipulating a
Document
object. These act on data in memory. The classjava.nio.channels.FileChannel
has similar methods that act directly on a file. Neither fine the end of lines automatically, but other classes injava.io
andjava.nio
do.您描述的操作在Swing世界中是标准的,而文本则归结为操纵Document对象。它们对内存中的数据起作用。 java.nio.channels.FileChannel类具有直接作用于文件的类似方法。既没有自动结束行,也没有java.io和java.nio中的其他类。
-
Apache Commons has a sandbox library called Flatfile which looks like it does what you want. The problem is that no code has been released yet. You may, however, want to talk to people working on it to get some more ideas. I didn't do a general check on libraries.
Apache Commons有一个名为Flatfile的沙箱库,它看起来像你想要的那样。问题是尚未发布任何代码。但是,您可能希望与工作人员交谈,以获得更多想法。我没有对图书馆进行一般性检查。
#4
Have you looked into File/FileReader/FileWriter/BufferedReader? You can get the contents of the files and manipulate it as you like, you can search the data in the files, you can overwrite files, create new, append to an existing....
你看过File / FileReader / FileWriter / BufferedReader吗?您可以获取文件的内容并根据需要进行操作,您可以搜索文件中的数据,可以覆盖文件,创建新文件,附加到现有文件....
I am not sure this is exactly what you are asking for but I use these APIs all the time for logging, RTF editors, text file creation for email, and many other things.
我不确定这正是您所要求的,但我一直使用这些API进行日志记录,RTF编辑器,电子邮件的文本文件创建以及许多其他内容。
As far as cut/copy/past goes, I have not come across the ability to do that directly, however, you can output the contents of the file and "copy" what part of it you want and "paste" it into a new file, or append it to an existing.
至于剪切/复制/过去,我没有遇到直接这样做的能力,但是,你可以输出文件的内容并“复制”你想要的那部分并将其“粘贴”到一个新的文件,或将其附加到现有文件。
#5
While writing a byte array to a file is a common task, writing to a give file 32-bytes byte array just once is just not something you are going to find in java.io :)
将字节数组写入文件是一项常见的任务,只写一次给定文件32字节字节数组只是你在java.io中找不到的东西:)
To get started, would the below method and comments look reasonable to you? I bet someone here, maybe even myself, could whip it out quick like.
首先,以下方法和评论对您来说是否合理?我打赌有人在这里,甚至是我自己,可以快速地鞭打它。
public static void writeFauxVirusSignature(File file, byte[] bytes, long offset) {
//open file
//move to offset
//write bytes
//close file
}
Questions:
- How big could the potential target files be?
- Do you need performance?
潜在的目标文件有多大?
你需要表现吗?
I ask because clean, easy to read code would use Apache Commons lib's, but large file writes in a performance sensitive environment will necessitate using java.nio
libraries
我问,因为干净,易于阅读的代码将使用Apache Commons lib,但在性能敏感的环境中进行大文件写入将需要使用java.nio库