Frage

Ich versuche, Schreibleitung in eine Datei in Zeile-Position n.

Auch wenn die Zeile n nicht vorhanden ist. In diesem Fall muss die Datei mit leeren Zeilen wachsen n zu erreichen. Im Grunde so etwas wie writer.writeLine(n, mycontent). myCONTENT ist eine binäre Darstellung von ObjectOutputStream. Jede Zeile der Datei enthält ein serialisierte Objekt. Die Zeilennummer ist der Index.

Wie kann ich zu einer bestimmten Zeile zu schreiben? -. Ohne FileUtils oder jegliche nicht-Standard-API-Komponenten mit

Diese Antwort ziemlich fasst up, was ich will - aber mit dem Schreiben scheint es anders zu verhalten.

Bearbeiten Ich erklärte meine Frage wegen der Kommentare

War es hilfreich?

Lösung

Ist der Begriff der Linie für Sie sehr wichtig ist? Sonst könnte man wahrscheinlich eine Karte in einer Datei, serialisiert und es verwenden, um Ihre Objekte zu einem bestimmten Index zu schreiben oder lesen (in diesem Fall würde der Indexschlüssel der Karte sein).

Hier ist ein kleines Beispiel.

    public static void main(String[] args) throws Exception {
    ObjectOutputStream tocStream = new ObjectOutputStream(new FileOutputStream("myfile.toc"));
    Map<Integer,Object> tableOfContent = new HashMap<Integer, Object>();
    String myString = "dataOne";
    Date myDate = new Date();
    tableOfContent.put(0,myDate);
    tableOfContent.put(1,myString);
    tocStream.writeObject(tableOfContent);
    tocStream.flush();
    tocStream.close();
    ObjectInputStream tocInputStream = new ObjectInputStream(new FileInputStream("myfile.toc"));
    Map<Integer,Object> restoredTableOfContent = (Map<Integer, Object>) tocInputStream.readObject();
    Object restoredMyString  =  restoredTableOfContent.get(1);
    System.out.println(restoredMyString);
    tocInputStream.close();
}

Andere Tipps

This won't work, because each serialized object might contain one or several newline character(s) as part of the its binary representation. So if you write a new object at line 3, you might very well write the object in the middle of the binary representation of your first one. Test it :

public class OOSTest {
    public static void main(String[] args) throws IOException {
        String s = "Hello\nWorld";
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(s);
        oos.flush();
        oos.close();
        System.out.println(new String(bos.toByteArray()));
    }
}

IMHO, you have three choices:

  1. Choose a protocol which will work with binary data (for example: for each object, write its length (in bytes) as an integer (4 bytes) followed with the bytes of the object itself;
  2. Encode the serialized bytes into a string, using Base64 for example, and use a separator between the objects which is not part of the encoding;
  3. Use an ObjectOutputStream to serialize an array of Objects. Writing a new entry would consist in deserializing the array, make sure it has the right size or copy it to a new array of the appropriate size, insert your object at its position, and reserialize the whole array.
  • Loop through the file reading the current values.
  • Write each value out to a new temp file.
  • If the old file ends before your line number keep writing out blank lines.
  • Then once you get to the line number you want to update write out the new data.
  • If there is more lines to the old file keep writing.
  • Delete the old file. Rename the temp file.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top