Datei kopieren mit Java

  • Java

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Radinator.

    Datei kopieren mit Java

    Hallo!,

    Ich versuche Java zu lernen und hab ein Problem. Ich möchte aus meiner .jar datei eine .txt datei in den Ordner kopieren. Der haken ist nur, ich möchte die Datei nur bis zur einer bestimmten byte länge kopieren. in vb ist das "einfach", aber in java brauch ich noch hilfe.

    VB-Code:

    VB.NET-Quellcode

    1. Public Sub Copy()
    2. Dim len As Integer = 858274 ' << nur bis zu den bytes kopieren :: den rest weglassen
    3. Dim SourceFile As New IO.FileStream("source", IO.FileMode.Open)
    4. KopierenMitByteLänge(SourceFile, Application.StartupPath & "\target.jar", len)
    5. End Sub
    6. Public Sub KopierenMitByteLänge(ByVal input As IO.Stream, ByVal targetFile As String, ByVal length As Integer)
    7. Dim buffer As Byte() = New Byte(8191) {}
    8. Using output As IO.Stream = IO.File.OpenWrite(targetFile)
    9. Dim bytesRead As Integer = 1
    10. While length > 0 AndAlso bytesRead > 0
    11. bytesRead = input.Read(buffer, 0, Math.Min(length, buffer.Length))
    12. output.Write(buffer, 0, bytesRead)
    13. length -= bytesRead
    14. End While
    15. End Using
    16. End Sub


    in Java konnte ich bislang nur den Code finden. Ich bin aber wohl zu doof den umzuschreiben :( Es wäre super nett wenn mir jemand hilft das zu lösen :)


    Spoiler anzeigen


    C#-Quellcode

    1. static public String ExportResource(String resourceName) throws Exception {
    2. InputStream stream = null;
    3. OutputStream resStreamOut = null;
    4. String jarFolder;
    5. try {
    6. stream = R78347fh97h.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
    7. if(stream == null) {
    8. throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
    9. }
    10. int readBytes;
    11. byte[] buffer = new byte[4096];
    12. jarFolder = new File(R78347fh97h.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
    13. resStreamOut = new FileOutputStream(jarFolder + resourceName);
    14. // int toReadBytes = Integer.parseInt(raw);
    15. while ((readBytes = stream.read(buffer)) > 0) {
    16. resStreamOut.write(buffer, 0, readBytes);
    17. }
    18. } catch (Exception ex) {
    19. throw ex;
    20. } finally {
    21. stream.close();
    22. resStreamOut.close();
    23. }
    24. return jarFolder + resourceName;
    25. }


    Wozu schreibt man ab, obwohl man es doch kann? :)

    ::Forum für Entwickler hier::
    Also in java würde ich mir aus der txt datei den text als string rausziehen, das bytearray von <string>.getBytes() dann mit FileOutputStream.write() dann schreiben. Da kannste eh das bytearray, das offset und die länge angeben(docs.oracle.com/javase/7/docs/…eam.html#write(byte%5B%5D, int, int)

    lg Radinator
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    danke schonmal für die antwort. das problem ist, es sollen nicht nur text-dateien, sondern auch .class dateien kopiert werden. Und am ende der datei stehen 3kb datenmüll und die soll er nicht mit kopieren, das ist halt mein problem.
    Wozu schreibt man ab, obwohl man es doch kann? :)

    ::Forum für Entwickler hier::
    Der code den du in vb und java servierst kommt mir iwie komisch vor...
    schau mal hier vorbei: openbook.rheinwerk-verlag.de/j…72-474a-bf5a-263de51866f3
    hier wird beschrieben, wie man ressourcen aus dem jar archiv laden kann. Ich gehe mal davon aus dass du die text datei in dem jar in den ordner, in dem die jar liegt, kopieren willst u d dass die so eine art ressource ist, wie du sie in vb über my.computer.resources erhalten kannst, ist
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell