aspose file tools*
The moose likes I/O and Streams and the fly likes copying contents of one file to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "copying contents of one file to another " Watch "copying contents of one file to another " New topic
Author

copying contents of one file to another

Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16

import java.io.*;
public class demo
{
public static void main(String args[])
{
try
{
FileReader fr = new FileReader("pqr.txt");
FileWriter fw = new FileWriter("xyz.txt");

int k;
while( ( k = fr.read() ) != -1 )
{
fw.write(k);
System.out.print((char) k);
}
fw.close();
fr.close();
}
catch(FileNotFoundException e)
{
System.out.println("File does not exist. " + e);
}
catch(IOException e)
{
System.out.println("Some I/O problem. " + e);
}
}
}


I am trying to copy contents of pqr.txt to xyz.txt. I have manually created a file pqr.txt in the same folder of class demo. I am getting the Error --- > File does not exist. java.io.FileNotFoundException: pqr.txt (The system cannot find the file specified)
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Please UseCodeTags when posting your code.

With the code that you're using to read a file, the txt file that you are reading must be placed in the root directory of your project (if you're using an IDE). If you are compiling and running it from command line, you just need to put the txt file in the same folder where your .class file is.

Also, couple of things as a side note: you should follow convention for naming classes (first letter always capital) and you should close streams (or free any resources whatsoever in other applications) in finally block or use try-with-resources statement introduced in latest JDK.


The quieter you are, the more you are able to hear.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Hi, welcome. regarding that exception, simply put, JVM is not finding your file. Make sure its on the path of class files and read access is granted to that file.

Edit: You need to follow some guidelines as Kemal pointed in order for others to understand your problem or solution easily


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16
I using Eclipse (version : juno release) , win7 64bit , Java7

C:\Users\John\workspace\FirstGUI\bin\pqr.txt
C:\Users\john\workspace\FirstGUI\bin\demo.class

both files are in same directory
what's Root directory here ? or how to grant read access to that file ?

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

C:\Users\John\workspace\FirstGUI is the root directory of your Eclipse project.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
If you ever need to find out which directory your system is using as root just add the following line to your code and see what prints to the console:

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Tony Docherty wrote:If you ever need to find out which directory your system is using as root just add the following line to your code and see what prints to the console:


Or:
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16
well thanks to Mr. Biswas, Mr. Docherty & Mr. Sokolovic

Mr. Sokolovic i followed your instruction , got success ( thumbs up). I placed the pqr.txt in root directory. After running the program it created the file xyz.txt with all contents copied.
Henceforth i will use code tags.

To find out which directory your system is using as root. Mr. Docherty & Mr. Sokolovic thumbs up for both
Andreas Hollmann
Greenhorn

Joined: Jan 06, 2010
Posts: 27
look at new nio package in jdk7. It should simplify working with files. For example in jdk7 you can work with Path classes instead of Strings for expressing urls to files and other ressources.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copying contents of one file to another