Results 1 to 14 of 14

Thread: Copying Files

  1. #1
    tgns223 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default Copying Files

    Hi All,

    Im looking for some assistance in writing a basic program which will copy the contents of a source file to a destination file. The program should prompt the user for both the source and destination file names as well.

    I know this should be simple but evidently Im not very good with Java.

    PLEASE HELP!!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Are source file and destination file on the same computer?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by tgns223 View Post
    Hi All,

    Im looking for some assistance in writing a basic program which will copy the contents of a source file to a destination file. The program should prompt the user for both the source and destination file names as well.

    I know this should be simple but evidently Im not very good with Java.

    PLEASE HELP!!
    Of course we can help you to solve this, but not in this. I don't think anyone wants to write code for you. If you really want to do that then paid and hire someone to do it.

    Show your effort, what you've done so far. Looking at that we can guide you. That's the idea about a forum.

    As a hint read Java IO API, you can find useful stuff.

  4. #4
    tgns223 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Yes the source and destination files are both on the same computer. I have this code below:

    Java Code:
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class copy {
      public static void main(String[] args) throws IOException {
        File inputFile = new File("in.txt");
        File outputFile = new File("out.txt");
    
        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        int c; 
    
        while ((c = in.read()) != -1)
          out.write(c);
    
        in.close();
        out.close();
      }
    }
    I keep receiving this error when running the program:

    Exception in thread "main" java.io.FileNotFoundException: in.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.io.FileReader.<init>(FileReader.java:55)
    at copy.main(copy.java:11)

    I know the file is in the same directory so Im not quite sure whats happening..
    Last edited by Eranga; 06-12-2010 at 03:03 AM. Reason: code tags added

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Try passing in the full path to "in.txt" to the File constructor

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    (The system cannot find the file specified)
    Where is the file? Where is your program? Are they in the same folder?

  7. #7
    tgns223 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Yes they are both in the same folder..

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Can you open a terminal/command prompt window, go the folder with the files, display the files in that folder, execute the java command with your program and then copy and paste everything from the window to this forum?

  9. #9
    tgns223 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Well when I opened the command prompt and displayed the directory listing of the folder with my files I noticed that the file was named "in.txt.txt" Not sure how that happened but I renamed it to "in.txt" and it works. :)

    Thanks for the help!!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    in.txt.txt - One of the gotchas from Microsoft's attempt to hide details from users.

    Go into the setup for Windows Explorer and change the setting to display the extensions of files.

  11. #11
    tgns223 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Gotta love windows. :) thanks again!

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by tgns223 View Post
    I know the file is in the same directory so Im not quite sure whats happening..
    As you sorted out here, exception point you what happen. In such a situation check with the basis like file exist, file name, etc. Sometime those minor things are really hard to track.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    in.txt.txt - One of the gotchas from Microsoft's attempt to hide details from users.

    Go into the setup for Windows Explorer and change the setting to display the extensions of files.
    Actually windows hide the right most extension only. If you don't remember that extensions are hide you think that file name is in.txt, not in.txt.txt, which is hard to track in case.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by tgns223 View Post
    Gotta love windows. :) thanks again!
    If you are satisfied with the solution please mark the thread solved. And also please code tags for posting code segments next time in the forum. Unformated codes are really hard to read. If you don't know how to do it, check on my signature.

Similar Threads

  1. Not Copying Libraries!?!
    By Atriamax in forum NetBeans
    Replies: 0
    Last Post: 10-13-2009, 07:56 AM
  2. problems copying a file
    By atom86 in forum Advanced Java
    Replies: 16
    Last Post: 10-01-2009, 02:40 PM
  3. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 07:03 AM
  4. Copying a derectory
    By linux1man in forum New To Java
    Replies: 10
    Last Post: 01-15-2009, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •