Warum kann man die kopierte Datei nicht öffnen?

Status
Nicht offen für weitere Antworten.

bernd

Bekanntes Mitglied
Mit folgendem Code lässt sich eine Datei kopieren, aber bei dem Versuch sie zu öffnen
erscheint nur eine leere Exel Mappe.
Die Größe beider Dateien ist allerdings die gleiche!

Code:
package Test;

import java.io.*;

public class Dateien
{
   public int i;
   public String Pfad = "C:\\Dokumente und Einstellungen\\SirKillelot\\Eigene Dateien\\EVL.xls";
   public String newfile = "C:\\Dokumente und Einstellungen\\SirKillelot\\Eigene Dateien\\Test\\EVL.xls";
   public void kopieren()
  {
            File file = new File(Pfad);
        try {
             BufferedReader in = new BufferedReader(new FileReader(new File(Pfad)));
             BufferedWriter out = new BufferedWriter(new FileWriter(newfile));

             while ((i = in.read()) != -1)
                   out.write(i);
                   in.close();
                   out.close();

            }catch (Exception e) {System.out.println( e );}

  }
}

Jetzt die main:

Code:
import Test.*;

/**
  *
  * Beschreibung.
  *
  * @version 1.0 vom 11.10.2004
  * @author
  */

public class Datei1 {

  public static void main(final String[] args) {
       Dateien m = new Dateien();
       m.kopieren();
  }
}

Was stimmt denn da nicht?
 
weil du es binär machen musst 🙂

Code:
InputStream in = new FileInputStream("C:\\test.xls");
OutputStream out = new FileOutputStream("C:\\testkopie.xls");
byte[] buffer = new byte[1024];
for (int n;(n = in.read(buffer)) != -1;out.write(buffer, 0, n));

in.close();
out.close();

achja, try catch musst noch hinzufügen 😉
 
Alles klar funktioniert! 🙂
Aber so gehts auch, ist nur noch die Frage was schneller kopiert?

Code:
package Test;

import java.io.*;

public class Dateien
{
   public int i;
   public String Pfad; // Pfad muss noch angepasst werden!
   public String newfile; // Pfad muss noch angepasst werden!
   
   public void kopieren()
  {
            File file = new File(Pfad);
        try {
              BufferedInputStream in = new BufferedInputStream(new FileInputStream(Pfad));
              BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newfile));
                while ( (i = in.read()) != -1)
                   out.write(i);
                   in.close();
                   out.close();

            }catch (Exception e) {System.out.println( e );}
  }
}
 
Und vergesst nicht, dass der Cache wesentlich schneller als eine Festplatte ist... das muss also nichteinmal langsamer sein :wink:
 
Beni hat gesagt.:
Und vergesst nicht, dass der Cache wesentlich schneller als eine Festplatte ist... das muss also nichteinmal langsamer sein :wink:

Hab das jetzt mal mit ner Datei von 700 MB ausprobiert, es scheint mir wirklich schneller zu sein!
Kann das einer bestätigen?
:meld:
 
Ich hab mal ein Testprogramm geschrieben:

Code:
import java.io.*;
import java.util.*;

public class CopyTest
{
  public static void main (String[] args) throws IOException
  {
    CopyTest ct = new CopyTest (new BufferedByteStreamMethod(), new ByteStreamMethod());
    File f = ct.writeTestFile (new File ("C:\\"), 1000000);
    File f2 = new File (f.getAbsolutePath() + "2");
    try{
      Thread.sleep (500);
    }catch (InterruptedException e){}
    ct.test (f, f2);
  }
  private CopyMethod cma;
  private CopyMethod cmb;
  public CopyTest (CopyMethod meth1, CopyMethod meth2)
  {
    cma = meth1;
    cmb = meth2;
  }
  public void test (File start, File ziel)
  {
    if (!start.exists()){
      throw new IllegalArgumentException ("Startfile doesn't exist! " + start, new FileNotFoundException());
    }
    if (ziel.exists()){
      ziel.delete();
    }
    long before = System.currentTimeMillis();
    try{
      cma.copy (start, ziel);
    }catch (Exception e){
      System.out.println("Exception testing class: " + cma.getClass().getName());
      e.printStackTrace();
      return;
    }
    long after = System.currentTimeMillis();
    long cmal = after - before;
    System.out.println(cma.getClass().getName() + " needed " + cmal + " milliseconds.");
    try{
      Thread.sleep (500);
    }catch (InterruptedException e){}
    before = System.currentTimeMillis();
    try{
      cmb.copy (start, ziel);
    }catch (Exception e){
      System.out.println("Exception testing class: " + cmb.getClass().getName());
      e.printStackTrace();
      return;
    }
    after = System.currentTimeMillis();
    long cmbl = after - before;
    System.out.println(cmb.getClass().getName() + " needed " + cmbl + " milliseconds.");
    System.out.println("Difference: " + Math.abs (cmal - cmbl));
  }
  public File writeTestFile (File parentDirectory, long size_kb) throws IOException
  {
    if (!parentDirectory.isDirectory()){
      throw new IllegalArgumentException ("Not a directory: " + parentDirectory);
    }
    File fil = new File (parentDirectory, "testfile.tst");
    if (fil.exists()){
      fil.delete();
    }
    byte[] buf = new byte[1024];
    Arrays.fill (buf, (byte)1);
    OutputStream os = new FileOutputStream (fil);
    byte lastPercent = -1;
    System.out.print("Write testfile: ___________\b\b\b\b\b\b\b\b\b\b\b"); //(Edit:verändert)
    for (long l = 0; l < size_kb; l++){
      os.write (buf);
      byte percent = (byte)((l * 1.0) / size_kb * 10);
      if (percent > lastPercent){
        lastPercent = percent;
        System.out.print("|");
      }
    }
    System.out.println("|"); //(Edit:verändert)
    os.close();
    return fil;
  }
}
abstract class CopyMethod
{
  abstract public void copy (File start, File ziel) throws IOException;
}
class ByteStreamMethod extends CopyMethod
{
  public void copy (File start, File ziel)
  throws IOException
  {
    InputStream in = new FileInputStream(start);
    OutputStream out = new FileOutputStream(ziel);
    byte[] buffer = new byte[1024];
    for (int n;(n = in.read(buffer)) != -1;out.write(buffer, 0, n));

    in.close();
    out.close();
  }
}
class BufferedByteStreamMethod extends CopyMethod
{
  public void copy (File start, File ziel)
  throws IOException
  {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(start));
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(ziel));
    int i;
    while ((i = in.read()) != -1)
      out.write(i);
    in.close();
    out.close();
  }
}

Ausgabe bei mir:
Write testfile: ||||||||||
BufferedByteStreamMethod needed 127687 milliseconds.
ByteStreamMethod needed 114969 milliseconds.
Difference: 12718

Wer mindestens 2 GB auf C:\ frei hat, kanns ja auch mal testen.
 
Ich habs ja gesagt...
Obwohl sich alles im Ram abspielt, ist es doch ein Arbeitsschritt mehr....


BuffredByteStream ist besser, wenn die Daten nicht regelmässig ankommen...(Dann sogar schneller)
ByteStream ist besser wenn die Daten regelmässig ankommen.... :meld:
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben