• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copying contents of one file to another

 
Johhn Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Johhn Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Users\John\workspace\FirstGUI is the root directory of your Eclipse project.
 
Tony Docherty
Bartender
Posts: 2971
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic