Announcement

Collapse
No announcement yet.

Schnelles kopieren von Dateien

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Schnelles kopieren von Dateien

    Hallo Leute,

    ich kopiere Dateien mit meinem Javatool mittels fileInputStream.getChannel() und fileOutputStream.getChannel(). Habe aber festgestellt, dass dies mit nur ca. 20 % der Leistung die möglich wäre passiert. Einstellungen an buffer und ähnlichen haben auch keine Auswirkung. Gibt es eine Möglichkeit mit Java dies zu beschleunigen. Z.B. anderer Algorithmus?

    Hier mal der Code:
    Code:
    public class FileCopy {
      private long chunckSizeInBytes = 1024 * 1024; //Standard: Buffer 1MB;
      private JobStatistic statistics;
      private File source;
      private File target;
      private long overallBytesTransfered = 0L;
      private boolean interupt = false;
    
      public FileCopy(File source, File target, JobStatistic statistics) {
        this.source     = source;
        this.target     = target;
        this.statistics = statistics;
      }
    
      public long copy() {
        FileInputStream fis  = null;
        FileOutputStream fos = null;
        try {
          fis = new FileInputStream(source);
          fos = new FileOutputStream(target);
          FileChannel inputChannel = fis.getChannel();
          FileChannel outputChannel = fos.getChannel();
          transfer(inputChannel, outputChannel, source.length());
          fis.close();
          fos.close();
          target.setLastModified(source.lastModified());
        }
        catch (Exception ex) {
          statistics.addLogEntry(ex.getMessage());
          statistics.addErrorFile();
          overallBytesTransfered = 0L;
          try {
            // Bei Fehler Versuche die Streams zu schließen
            fis.close();
            fos.close();
          }
          catch (Exception ex2) {
    
          }
        }
        return overallBytesTransfered;
      }
    
      public void transfer(FileChannel fileChannel, ByteChannel byteChannel, long lengthInBytes) throws IOException {
        long overallBytesTransfered = 0L;
        while (overallBytesTransfered < lengthInBytes) {
          if(!interupt){
            long bytesTransfered = 0L;
            bytesTransfered = fileChannel.transferTo(overallBytesTransfered, Math.min(chunckSizeInBytes, lengthInBytes - overallBytesTransfered), byteChannel);
            overallBytesTransfered += bytesTransfered;
          }
          else
            break;
        }
        this.overallBytesTransfered = overallBytesTransfered;
      }
    
      public void breakCopy(){
          interupt = true;
      }
    
    
    }
    Zuletzt editiert von alexdgg; 15.04.2011, 09:04.
    AlexDgG

    Es gibt keine dummen Fragen. Nur dumme Antworten!

  • #2
    Versuche, zu puffern

    Im Zusammenhang mit Zugriff auf Files gibt es die Empfehlung, die Zugriffe nicht direkt über die FileXXStreams zu machen, sondern diese noch zu puffern. Das geht, indem man die FileXXStreams mit BufferedXXStreams dekoriert. Der Code für die Erzeugung sieht in etwa so aus:
    Code:
    final InputStream in = new BufferedInputStream(new FileInputStream("pfadZurDatei"));
    Gruß ngomo
    http://www.winfonet.eu

    Comment

    Working...
    X