Results 1 to 3 of 3
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Transferring a .txt file from 1 location to another

    I have the code for creating a folder in a set location

    Java Code:
    File f = new File("path in here");
              f.mkdir();
    Now I am trying to work out the easiest way on the click of a second button to copy a .txt file from 1 location on my c drive to the folder that the previous code created, I have the path etc just cant seem to get the syntax right

    Thanks in advance

  2. #2
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Move a file - delete original - not what i want

    Ok I now am able to transfer the .txt file from 1 location to another but it deletes the original which I dont want I just want to copy it

    Java Code:
    File a = new File ("path......");
              a.renameTo(new File("path....\\filename.txt"));
    Anyone help?

    Thanks

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

    Default

    What you have to do is,

    1. Create two new file instance, one for the source and one for the destination.

    Java Code:
    File sou = new File(souFile);
    File des = new File(desFile);
    The InputStream instance for input and OutputStream for output file object.

    Java Code:
    InputStream inStr = new FileInputStream(sou);
    OutputStream outStr = new FileOutputStream(des);
    Finally create a byte buffer to read the file.

    Java Code:
    byte[] readBuf = new byte[2048]; // Depends on your file size
    outStr.write(readBuf, 0, #of_bytes_reads);
    Hope this is clear, If not let me know.

Similar Threads

  1. Netbeans errors when transferring file
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 04-15-2009, 04:41 PM
  2. Java logging - log file location for FileHandler
    By kfir.wolfson@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 03-24-2009, 09:22 AM
  3. weird location of ovals
    By diggitydoggz in forum New To Java
    Replies: 6
    Last Post: 12-24-2008, 01:25 AM
  4. Transferring Over Network
    By qwerty in forum Advanced Java
    Replies: 2
    Last Post: 08-01-2008, 02:40 PM
  5. how to get the location of some button
    By mary in forum Java 2D
    Replies: 2
    Last Post: 08-05-2007, 05:02 AM

Posting Permissions

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