I am trying to copy a file in the path "c:\temp\source.txt" to "d:\temp\destination.txt"
Below is the code written by me. It's getting compiled but saying
java.io.FileNotFoundException: c:\temp\source.txt (The system cannot find the fi le specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at fileCopy1.main(fileCopy1.java:13)
------------------------------------------- ------- Also when i use exists src.exists(); it's returning false.
Some one please help me...
Note: Please note that i don't want to use command line arguments as i have to read from a file.
import java.io.*;
public class fileCopy1 {
fileCopy1() { }
public static void main(String args[]) {
try {
File sourceFile=new File("c:/temp/source.txt"); // source BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile), 4096); File targetFile = new File("d:/temp/destination.txt"); // destination BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile), 4096); int theChar; while ((theChar = bis.read()) != -1) { bos.write(theChar); } bos.close(); bis.close(); System.out.println ("copy done!");
But: If you want to access the root directory of drive C, you MUST write "c:\\" The separator "/" works fine for subdirectories.
In this case you used a relative path instead of a absolute path.
I assume you launched the java program from a project dir like c:\myprojects\filecopy
If you don't use "c:\\" the used filepath is c:\myprojects\filecopy\temp\src.txt
Try to print out the filepath of your sourcefile after you created it.
The syntax "c:\" is only used by dos and windows.
Hint: Use java.io.File.separatorChar or java.io.File.separator to get the system dependent separator. This increases compatibility with other software.