• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

copying contents of one file to another

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C:\Users\John\workspace\FirstGUI is the root directory of your Eclipse project.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Onion rings are vegetable donuts. Taste this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic