Results 1 to 8 of 8
- 04-09-2011, 10:59 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
i've checked the file permissions and apparently I have full control over itJava 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 am the system admin and i've tried coyping to and from different folders.
I am using windows 7
here's the error.
I appreciate the help.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)
Many thanks in advance.
- 04-09-2011, 11:04 AM #2
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
I think you are supposed to write instead of
public static void main(String[] args) throws FileNotFoundException, IOExceptionpublic static void main(String[] args) throws IOException
- 04-09-2011, 11:07 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
attempted that.. exception persists :C
thanks for the help
-
When you specify a directory, specify the directory up to the name i.e.
so make sure the path you enter doesn't end with a '/' otherwise it will look for a file or folder named "" within that pathJava Code://correct directory name String dir = "C:/Program Files/MyProgram" //throws FileNotFound ex String dir = "C:/Program Files/MyProgram/"
-
As for the access denied, make sure the folder/file is not Read-Only first.
- 04-09-2011, 11:24 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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
- 04-09-2011, 01:19 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 04-09-2011, 01:23 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
java.io.FileNotFoundException: ..\log\server.log (Access is denied.
By parimal in forum Advanced JavaReplies: 22Last Post: 04-09-2011, 04:59 AM -
Access is denied when trying to create a file.
By adwodon in forum New To JavaReplies: 5Last Post: 03-18-2011, 02:58 PM -
Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02-10-2011, 09:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks