Results 1 to 8 of 8
  1. #1
    Valemore is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default java copying a file results in access denied exception

    Hi, newbie here, first time posting
    I decided to start playing arround with the fileinputstreams and outputstreams and wrote a program that copies a file to another folder, but it always throws a FileNotFound: access denied exception.

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Scanner;
    
    
    public class filecopy {
    
    	public static void main(String[] args) throws IOException {
    		   	Scanner in=new Scanner(System.in);
    			System.out.println("So..i've heard you like to copy files?");
    		   	System.out.println("specify source file path");
    			String source=in.next();
    			System.out.println("specify destination file path");
    			String target=in.next();
    		   	InputStream inn = new FileInputStream(source);
    	                OutputStream out = new FileOutputStream(target);
    	                byte[] buf = new byte[2048];
    	                int len;
    	                while((len=inn.read(buf))>0){
    	        	       out.write(buf, 0, len);
    	                }
    	                inn.close();
    	                out.close();
    
    	}
    
    }
    i've checked the file permissions and apparently I have full control over it
    i am the system admin and i've tried coyping to and from different folders.
    I am using windows 7

    here's the error.
    Java Code:
    Exception in thread "main" java.io.FileNotFoundException: C: (Access is denied)
    	at java.io.FileOutputStream.open(Native Method)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at filecopy.main(filecopy.java:20)
    I appreciate the help.

    Many thanks in advance.

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    I think you are supposed to write instead of
    public static void main(String[] args) throws IOException
    public static void main(String[] args) throws FileNotFoundException, IOException

  3. #3
    Valemore is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    attempted that.. exception persists :C
    thanks for the help

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    6

    Default

    When you specify a directory, specify the directory up to the name i.e.

    Java Code:
    //correct directory name
    String dir = "C:/Program Files/MyProgram"
    
    //throws FileNotFound ex
    String dir = "C:/Program Files/MyProgram/"
    so make sure the path you enter doesn't end with a '/' otherwise it will look for a file or folder named "" within that path

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    6

    Default

    As for the access denied, make sure the folder/file is not Read-Only first.

  6. #6
    Valemore is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    ahh thanks, im so dumb lol
    I had the destination folder path end with the folder name and didn't specify the file.
    for example i had
    source= C:\Myfolder\file.txt
    target=C:\Myfolder

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,287
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by Valemore View Post
    here's the error.
    Java Code:
    Exception in thread "main" java.io.FileNotFoundException: C: (Access is denied)
    Your program is trying to open "C:" which isn't a regular file, it is your entire hard disk.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,287
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by baf06 View Post
    I think you are supposed to write instead of


    public static void main(String[] args) throws FileNotFoundException, IOException
    That is not true: the FileNotFoundException class extends the IOException class so only the latter needs to be specified in the throws clause.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 22
    Last Post: 04-09-2011, 04:59 AM
  2. Access is denied when trying to create a file.
    By adwodon in forum New To Java
    Replies: 5
    Last Post: 03-18-2011, 03:58 PM
  3. Replies: 6
    Last Post: 02-10-2011, 10:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •