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! :)