2016-05-04 6 views
0

zu kopieren Ich versuche, eine einfache TXT-Daten mit RandomAccessFile in Java zu kopieren. Ive codiert das folgende Programm:Ich versuche, eine Datei mit RandomAccessFile in Java

Meine Haupt Methode:

public static void main(String args[]){ 
    File original = new File("C:\\Users\\Tjatte\\Documents\\testdatei.txt"); 
    File copy = new File("C:\\Users\\Tjatte\\Documents\\testdateicopy.txt"); 
    copy(original,copy); 
} 

Meine statische Kopie Methode

public static void copy(File main,File copy){ 
    try{ 
     RandomAccessFile data = new RandomAccessFile(main,"rw"); 
     RandomAccessFile datacopy = new RandomAccessFile(copy,"rw"); 

     datacopy.seek(0); 
     for(int i = 0; i < main.length(); i++){ 
      datacopy.write(data.read()); 
      data.skipBytes(i); 
     } 
    }catch(IOException e){ 
    } 
} 

Jedes Mal, wenn ich möchte eine Datei mit dem Text „Hallo“ in sie kopieren, die Ausgabe in der kopierten Datei ist "halÿÿ" ... Aber es muss "Hallo" sein.

Ich freue mich über jede Hilfe! Danke im Voraus.

+0

Bitte überprüfen Sie es [hier] (https://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/) – Rao

Antwort

0

Überspringen Sie keine Bytes. Scheint wie "Lese" Operation wird den Zeiger automatisch zum nächsten Zeichen bewegen.

Löschen Sie einfach die Zeile mit "data.skipBytes (i);"

+0

Danke für Ihre Hilfe, es funktioniert . :) – Tjatte

+0

Ich bin froh, dass ich helfen konnte! :) Können Sie die Antwort dann akzeptieren? – Igor

+0

Ja sicher ... dauerte eine Weile, bis ich es gefunden habe, aber hier gehen wir :) – Tjatte

1

Ich würde Files.copy(Path, OutputStream) bevorzugen und ein try-with-resources close wie

public static void copy(File main, File copy) { 
    try (OutputStream fos = new FileOutputStream(copy)) { 
     Files.copy(main.toPath(), fos); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 
} 

oder, wie @Andreas in den Kommentaren darauf hingewiesen, verwenden Files.copy(Path, Path, CopyOption...) wie

try { 
    Files.copy(main.toPath(), copy.toPath()); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
+1

Warum nicht die ['Files.copy (Pfadquelle, Pfadziel)]' (https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy%) verwenden 28java.nio.file.Path,% 20java.nio.file.Path,% 20java.nio.file.CopyOption ...% 29) Methode? – Andreas

+0

Ja, normalerweise hätte ich versucht, eine andere Lösung anstelle von RandomAccessFile .. Aber meine Aufgabe für die Universität wurde auf RandomAccessFile .. eingeschränkt :) – Tjatte

+0

@Andreas Ich vergaß, dass die Vararg optional war. –

0

Sie können auf diese Weise auch:

public class CopyFileContent{ 

    static final String FILEPATH = "C:../../inputfile.txt"; 

    public static void main(String[] args) { 

     try { 

      System.out.println(new String(readFromFile(FILEPATH, 150, 23))); 

      writeToFile(FILEPATH, "Hello", 22); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    private static byte[] readFromFile(String filePath, int position, int size) 
      throws IOException { 

     RandomAccessFile file = new RandomAccessFile(filePath, "r"); 
     file.seek(position); 
     byte[] bytes = new byte[size]; 
     file.read(bytes); 
     file.close(); 
     return bytes; 

    } 

    private static void writeToFile(String filePath, String data, int position) 
      throws IOException { 

     RandomAccessFile file = new RandomAccessFile(filePath, "rw"); 
     file.seek(position); 
     file.write(data.getBytes()); 
     file.close(); 

    } 
} 
+0

Danke für Ihre Arbeit, schätzen Sie es. Momentan bin ich am Anfang von Streams, aber deine Mühe wird mir helfen, mich zu verbessern! – Tjatte

+0

Bitte stimmen Sie ab, wenn meine Lösung korrekt ist, da es anderen mit ähnlichem Problem helfen würde –

+0

ye ich tat, aber ich habe im Moment nicht genug Reputation. :/ – Tjatte

Verwandte Themen