Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

One question about I/O FileInputStream and FileOutputStream

807606Mar 25 2007 — edited Mar 25 2007
I want to implement a method which can copy one file to another file by byte transmission . My code is as following:
public class Hai {
    public static void main(String[] args) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
        	File test =  new File ("C:\\Documents and Settings\\Hai\\My Documents");
        	File test1 =  new File ("C:\\Documents and Settings\\Hai\\My Documents");
            in = new FileInputStream(test);
            out = new FileOutputStream(test1);
            int c;

            while ((c = in.read()) != -1) {
                out.write(c);
            }

          } finally {
              if (in != null) {
                in.close();
              }
              if (out != null) {
                out.close();
              }
        }
    }
}
My OS is WinXP, the original is named Hai ( test, File test = new File ("C:\\Documents and Settings\\Hai\\My Documents"))

the destination file is Ren (test1, File test1 = new File ("C:\\Documents and Settings\\Hai\\My Documents"))

When I run above code, I always get
Exception in thread "main" java.io.FileNotFoundException: C:\Documents and Settings\Hai\My Documents (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at Test.Hai.main(Hai.java:15)

How should I fix this problem? Thanks! :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 22 2007
Added on Mar 25 2007
4 comments
262 views