Dr_Cox1911
Cadet 4th Year
- Registriert
- Sep. 2011
- Beiträge
- 114
Hab da grad ein etwas komisches Problem und komm einfach nicht auf den Fehler.
Möchte in Java einen Ordner samt Inhalt an eine andere Stelle kopieren und anschließend den Quellordner löschen.
Hier ist der Code für das Kopieren:
und hier der Aufruf (mit Beispielpfaden, natürlich wurde vorher eine Instanz von CopyDirectory gebildet):
Warum wirft er mir hier eine NullPointerException?
So wie es aussieht, schafft hängt er sich hier auf:
Möchte in Java einen Ordner samt Inhalt an eine andere Stelle kopieren und anschließend den Quellordner löschen.
Hier ist der Code für das Kopieren:
Code:
public class CopyDirectory {
private BufferedInputStream in = null;
private BufferedOutputStream out = null;
public void copyDir(File quelle, File ziel) throws FileNotFoundException, IOException {
System.out.println(quelle.list());
File[] files = quelle.listFiles();
ziel.mkdirs();
for (File file : files) {
if (file.isDirectory()) {
copyDir(file, new File(ziel.getAbsolutePath() + System.getProperty("file.separator") + file.getName()));
}
else {
copyFile(file, new File(ziel.getAbsolutePath() + System.getProperty("file.separator") + file.getName()));
}
}
}
public void copyFile(File file, File ziel) throws FileNotFoundException, IOException {
System.out.println("Copy " + file.getAbsolutePath() + " to " + ziel.getAbsolutePath());
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(new FileOutputStream(ziel, true));
int bytes = 0;
while ((bytes = in.read()) != -1) {
out.write(bytes);
}
in.close();
out.close();
}
public boolean del(File dir){
if (dir.isDirectory()){
String[] entries = dir.list();
for (int x=0;x<entries.length;x++){
File aktFile = new File(dir.getPath(),entries[x]);
del(aktFile);
}
if (dir.delete())
return true;
else
return false;
}
else{
if (dir.delete())
return true;
else
return false;
}
}
Code:
File quelle = new File("F:\test");
File ziel = new File("E:\test");
try {
copydir.copyDir(quelle, ziel);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
So wie es aussieht, schafft hängt er sich hier auf:
Code:
System.out.println(quelle.list());
File[] files = quelle.listFiles();
Zuletzt bearbeitet: