The Artima Developer Community
Sponsored Link

Java Answers Forum
java io question - pls help

1 reply on 1 page. Most recent reply: Jul 4, 2005 10:55 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
kiran kumar

Posts: 1
Nickname: kiran80
Registered: Jul, 2005

java io question - pls help Posted: Jul 4, 2005 9:39 AM
Reply to this message Reply
Advertisement
Hi all,

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!");

}
catch (Exception ex) {
ex.printStackTrace();
}

}

}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: java io question - pls help Posted: Jul 4, 2005 10:55 PM
Reply to this message Reply
The code is fine:

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.

Flat View: This topic has 1 reply on 1 page
Topic: Declaration Previous Topic   Next Topic Topic: how do we store a hashtable in Cache database

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use