Fehler bei Kopieren

Vale

Grünschnabel
Code:
Exception in thread "main" java.io.FileNotFoundException: C:\Users\Valentin\Desktop (Zugriff verweigert)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at Autostart.copyFile(Autostart.java:22)
    at Autostart.main(Autostart.java:14)


Das ist die Fehlermeldung wenn ich dieses Programm ausführe:

Java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;



public class Autostart {

    public static void main(String[] args) throws IOException { 
           File inF = new File("C:/Users/Valentin/Desktop/hs_err_pid1144.log"); 
           File outF = new File("C:/Users/Valentin/Desktop"); 
           copyFile(inF, outF); 
       } 
       
       public static void copyFile(File in, File out) throws IOException { 
           FileChannel inChannel = null; 
           FileChannel outChannel = null; 
           try { 
               inChannel = new FileInputStream(in).getChannel(); 
               outChannel = new FileOutputStream(out).getChannel(); 
               inChannel.transferTo(0, inChannel.size(), outChannel); 
           } catch (IOException e) { 
               throw e; 
           } finally { 
               try { 
                   if (inChannel != null) 
                       inChannel.close(); 
                   if (outChannel != null) 
                       outChannel.close(); 
               } catch (IOException e) {} 
           } 
           inChannel.close();
           outChannel.close();
       } 
}


Brauche dringend Hilfe!
Danke im vorraus..

Vale
 
Zuletzt bearbeitet von einem Moderator:
Hi und Willkommen bei tutorials.de :)

Diese zwei Zeilen sind das Problem:
Java:
File outF = new File("C:/Users/Valentin/Desktop");
...
outChannel = new FileOutputStream(out).getChannel();
Bei der anderen Datei (aus der gelesen wird) gibst du einen kompletten Pfad, also Ordner und Dateiname (hs_err_pid1144.log) an. Beim gezeigten Codestück ist aber nur ein Ordner, und diesen kompletten Ordner willst du jetzt mit deiner Dateikopie überschreiben.

Einfach einen Namen angeben, dann sollte es passen:
Java:
File outF = new File("C:/Users/Valentin/Desktop/KopieVonMeinerDatei.log");

PS: Solche Codeboxen macht man mit
[code=java] dein code [/code]
 

Neue Beiträge

Zurück