views:

269

answers:

5

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?

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.

Sorry for not RTFM well; I haven't come across any information, and I've been looking for a while now.

A: 

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....

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.

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.

northpole
+4  A: 

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.

MicSim
Yup, that's amazing. That's exactly what I want.
montooner
+1  A: 

As far as I know, Java has primarily lower level functions for manipulating files directly. Here is the best I've come up with

  1. 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 class java.nio.channels.FileChannel has similar methods that act directly on a file. Neither fine the end of lines automatically, but other classes in java.io and java.nio do.

  2. 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.

Kathy Van Stone
A: 

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 :)

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

Stu Thompson
+3  A: 

Java's RandomAccessFile is exactly what you want.

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.

jjnguy
RandomAccessFile might serve as a basis (personally I'd use a FileChannel), but there's a lot of work necessary to be able to remove or insert data in the middle of a file.
erickson
True, but it is a start...
jjnguy