Renaming and Moving Files: - Java

This is a discussion on Renaming and Moving Files: - Java ; So like, I know that when you copy a file, you do something along the lines of: FileChannel ic = new FileInputStream("source.txt").getChannel(); FileChannel oc = new FileOutputStream("target.txt").getChannel(); ic.transferTo(0, ic.size(), oc); ic.close(); oc.close(); However, I have no intention of copying a ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Renaming and Moving Files:

  1. Default Renaming and Moving Files:

    So like, I know that when you copy a file, you do something along the
    lines of:

    FileChannel ic = new FileInputStream("source.txt").getChannel();
    FileChannel oc = new FileOutputStream("target.txt").getChannel();
    ic.transferTo(0, ic.size(), oc);
    ic.close();
    oc.close();

    However, I have no intention of copying a file-at the end of my
    program, I'd like to rename one file and move another. How do I
    accomplish this?

    By the way, I tried something along the lines of:

    res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    "_results.txt")));

    so could someone explain the proper way to use the method or recommend
    a better one?

    Thanks!

    (Again, just how to MOVE and RENAME a file on the hardisk through
    Java)
    -The Duck

  2. Default Re: Renaming and Moving Files:

    On 7 Sie, 16:39, Danger_Duck <ganggang3s...@gmail.com> wrote:
    > So like, I know that when you copy a file, you do something along the
    > lines of:
    >
    > FileChannel ic = new FileInputStream("source.txt").getChannel();
    > FileChannel oc = new FileOutputStream("target.txt").getChannel();
    > ic.transferTo(0, ic.size(), oc);
    > ic.close();
    > oc.close();
    >
    > However, I have no intention of copying a file-at the end of my
    > program, I'd like to rename one file and move another. How do I
    > accomplish this?
    >
    > By the way, I tried something along the lines of:
    >
    > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > "_results.txt")));
    >
    > so could someone explain the proper way to use the method or recommend
    > a better one?
    >
    > Thanks!
    >
    > (Again, just how to MOVE and RENAME a file on the hardisk through
    > Java)
    > -The Duck


    Try to use java.io.File.renameTo(File) method.
    Note that moving file "might not succeed if a file with the
    destination abstract pathname already exists" (http://java.sun.com/
    javase/6/docs/api/java/io/File.html).

    Przemek

  3. Default Re: Renaming and Moving Files:

    Danger_Duck wrote:
    > So like, I know that when you copy a file, you do something along the
    > lines of:
    >
    > FileChannel ic = new FileInputStream("source.txt").getChannel();
    > FileChannel oc = new FileOutputStream("target.txt").getChannel();
    > ic.transferTo(0, ic.size(), oc);
    > ic.close();
    > oc.close();
    >
    > However, I have no intention of copying a file-at the end of my
    > program, I'd like to rename one file and move another. How do I
    > accomplish this?
    >
    > By the way, I tried something along the lines of:
    >
    > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > "_results.txt")));
    >
    > so could someone explain the proper way to use the method or recommend
    > a better one?
    >
    > Thanks!
    >
    > (Again, just how to MOVE and RENAME a file on the hardisk through
    > Java)
    > -The Duck

    You might want to look at File - it has builtins that do what you want:

    File file = new File("path to the file");
    file. \\delete(), renameTo(), etc.


    --
    Dave Miller
    Java Web Hosting at:
    http://www.cheap-jsp-hosting.com/

  4. Default Re: Renaming and Moving Files:

    Danger_Duck wrote:
    > So like, I know that when you copy a file, you do something along the
    > lines of:
    >
    > FileChannel ic = new FileInputStream("source.txt").getChannel();
    > FileChannel oc = new FileOutputStream("target.txt").getChannel();
    > ic.transferTo(0, ic.size(), oc);
    > ic.close();
    > oc.close();
    >
    > However, I have no intention of copying a file-


    So the first ten lines of your message are just starchy
    filler, right?

    > at the end of my
    > program, I'd like to rename one file and move another. How do I
    > accomplish this?
    >
    > By the way, I tried something along the lines of:
    >
    > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > "_results.txt")));


    Well, that'd be the way to rename a file, assuming res is a File
    and resultsLocation is something with a concat() method that returns
    a String or a URI. What happened when you tried it?

    > so could someone explain the proper way to use the method or recommend
    > a better one?
    >
    > Thanks!
    >
    > (Again, just how to MOVE and RENAME a file on the hardisk through
    > Java)


    To rename a file, see above. To move a file -- well, you'd
    better describe what you mean by "move."

    --
    Eric.Sosman@sun.com



  5. Default Re: Renaming and Moving Files:

    On Aug 7, 11:16 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
    > Danger_Duck wrote:
    > > So like, I know that when you copy a file, you do something along the
    > > lines of:

    >
    > > FileChannel ic = new FileInputStream("source.txt").getChannel();
    > > FileChannel oc = new FileOutputStream("target.txt").getChannel();
    > > ic.transferTo(0, ic.size(), oc);
    > > ic.close();
    > > oc.close();

    >
    > > However, I have no intention of copying a file-

    >
    >      So the first ten lines of your message are just starchy
    > filler, right?
    >
    > > at the end of my
    > > program, I'd like to rename one file and move another. How do I
    > > accomplish this?

    >
    > > By the way, I tried something along the lines of:

    >
    > > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > > "_results.txt")));

    >
    >      Well, that'd be the way to rename a file, assuming res is a File
    > and resultsLocation is something with a concat() method that returns
    > a String or a URI.  What happened when you tried it?
    >
    > > so could someone explain the proper way to use the method or recommend
    > > a better one?

    >
    > > Thanks!

    >
    > > (Again, just how to MOVE and RENAME a file on the hardisk through
    > > Java)

    >
    >      To rename a file, see above.  To move a file -- well, you'd
    > better describe what you mean by "move."
    >
    > --
    > Eric.Sos...@sun.com


    When I tried (res is the existing file),
    res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > "_results.txt")))


    I indeed found a new file with the name I specified. Unfortunately, it
    was blank, as the 0kb size confirmed. That's why I either need help
    with renameTo or a better method.

    By the way, res already exists (with contents and 1kb in size), and
    was opened using:
    File res = new File(resultsLocation.concat("/results.txt"));

    I'm on a Windows XP by the way, if that matters...

  6. Default Re: Renaming and Moving Files:

    On 7 Sie, 17:30, Danger_Duck <ganggang3s...@gmail.com> wrote:
    > On Aug 7, 11:16 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
    >
    >
    >
    > > Danger_Duck wrote:
    > > > So like, I know that when you copy a file, you do something along the
    > > > lines of:

    >
    > > > FileChannel ic = new FileInputStream("source.txt").getChannel();
    > > > FileChannel oc = new FileOutputStream("target.txt").getChannel();
    > > > ic.transferTo(0, ic.size(), oc);
    > > > ic.close();
    > > > oc.close();

    >
    > > > However, I have no intention of copying a file-

    >
    > >      So the first ten lines of your message are just starchy
    > > filler, right?

    >
    > > > at the end of my
    > > > program, I'd like to rename one file and move another. How do I
    > > > accomplish this?

    >
    > > > By the way, I tried something along the lines of:

    >
    > > > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > > > "_results.txt")));

    >
    > >      Well, that'd be the way to rename a file, assuming res is a File
    > > and resultsLocation is something with a concat() method that returns
    > > a String or a URI.  What happened when you tried it?

    >
    > > > so could someone explain the proper way to use the method or recommend
    > > > a better one?

    >
    > > > Thanks!

    >
    > > > (Again, just how to MOVE and RENAME a file on the hardisk through
    > > > Java)

    >
    > >      To rename a file, see above.  To move a file -- well, you'd
    > > better describe what you mean by "move."

    >
    > > --
    > > Eric.Sos...@sun.com

    >
    > When I tried (res is the existing file),
    > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    >
    > > "_results.txt")))

    >
    > I indeed found a new file with the name I specified. Unfortunately, it
    > was blank, as the 0kb size confirmed. That's why I either need help
    > with renameTo or a better method.
    >
    > By the way, res already exists (with contents and 1kb in size), and
    > was opened using:
    > File res = new File(resultsLocation.concat("/results.txt"));
    >
    > I'm on a Windows XP by the way, if that matters...


    Try to use java.io.File.exists() method to ensure that source file
    exists and target does not.
    I am not not sure about XP, but maybe there is problem with file
    separator character: '/'.

    Przemek

  7. Default Re: Renaming and Moving Files:

    On Aug 7, 11:49 am, "tomaszewski.p" <kssw...@gmail.com> wrote:
    > On 7 Sie, 17:30, Danger_Duck <ganggang3s...@gmail.com> wrote:
    >
    >
    >
    > > On Aug 7, 11:16 am, Eric Sosman <Eric.Sos...@sun.com> wrote:

    >
    > > > Danger_Duck wrote:
    > > > > So like, I know that when you copy a file, you do something along the
    > > > > lines of:

    >
    > > > > FileChannel ic = new FileInputStream("source.txt").getChannel();
    > > > > FileChannel oc = new FileOutputStream("target.txt").getChannel();
    > > > > ic.transferTo(0, ic.size(), oc);
    > > > > ic.close();
    > > > > oc.close();

    >
    > > > > However, I have no intention of copying a file-

    >
    > > >      So the first ten lines of your message are just starchy
    > > > filler, right?

    >
    > > > > at the end of my
    > > > > program, I'd like to rename one file and move another. How do I
    > > > > accomplish this?

    >
    > > > > By the way, I tried something along the lines of:

    >
    > > > > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > > > > "_results.txt")));

    >
    > > >      Well, that'd be the way to rename a file, assuming res is a File
    > > > and resultsLocation is something with a concat() method that returns
    > > > a String or a URI.  What happened when you tried it?

    >
    > > > > so could someone explain the proper way to use the method or recommend
    > > > > a better one?

    >
    > > > > Thanks!

    >
    > > > > (Again, just how to MOVE and RENAME a file on the hardisk through
    > > > > Java)

    >
    > > >      To rename a file, see above.  To move a file -- well, you'd
    > > > better describe what you mean by "move."

    >
    > > > --
    > > > Eric.Sos...@sun.com

    >
    > > When I tried (res is the existing file),
    > > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +

    >
    > > > "_results.txt")))

    >
    > > I indeed found a new file with the name I specified. Unfortunately, it
    > > was blank, as the 0kb size confirmed. That's why I either need help
    > > with renameTo or a better method.

    >
    > > By the way, res already exists (with contents and 1kb in size), and
    > > was opened using:
    > > File res = new File(resultsLocation.concat("/results.txt"));

    >
    > > I'm on a Windows XP by the way, if that matters...

    >
    > Try to use java.io.File.exists() method to ensure that source file
    > exists and target does not.
    > I am not not sure about XP, but maybe there is problem with file
    > separator character: '/'.
    >
    > Przemek


    Hmm, I'll try "\\" instead and see how it goes...

  8. Default Re: Renaming and Moving Files:

    On Aug 7, 11:49 am, "tomaszewski.p" <kssw...@gmail.com> wrote:
    > On 7 Sie, 17:30, Danger_Duck <ganggang3s...@gmail.com> wrote:
    >
    >
    >
    > > On Aug 7, 11:16 am, Eric Sosman <Eric.Sos...@sun.com> wrote:

    >
    > > > Danger_Duck wrote:
    > > > > So like, I know that when you copy a file, you do something along the
    > > > > lines of:

    >
    > > > > FileChannel ic = new FileInputStream("source.txt").getChannel();
    > > > > FileChannel oc = new FileOutputStream("target.txt").getChannel();
    > > > > ic.transferTo(0, ic.size(), oc);
    > > > > ic.close();
    > > > > oc.close();

    >
    > > > > However, I have no intention of copying a file-

    >
    > > >      So the first ten lines of your message are just starchy
    > > > filler, right?

    >
    > > > > at the end of my
    > > > > program, I'd like to rename one file and move another. How do I
    > > > > accomplish this?

    >
    > > > > By the way, I tried something along the lines of:

    >
    > > > > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
    > > > > "_results.txt")));

    >
    > > >      Well, that'd be the way to rename a file, assuming res is a File
    > > > and resultsLocation is something with a concat() method that returns
    > > > a String or a URI.  What happened when you tried it?

    >
    > > > > so could someone explain the proper way to use the method or recommend
    > > > > a better one?

    >
    > > > > Thanks!

    >
    > > > > (Again, just how to MOVE and RENAME a file on the hardisk through
    > > > > Java)

    >
    > > >      To rename a file, see above.  To move a file -- well, you'd
    > > > better describe what you mean by "move."

    >
    > > > --
    > > > Eric.Sos...@sun.com

    >
    > > When I tried (res is the existing file),
    > > res.renameTo(new File(resultsLocation.concat("/" + test.getName() +

    >
    > > > "_results.txt")))

    >
    > > I indeed found a new file with the name I specified. Unfortunately, it
    > > was blank, as the 0kb size confirmed. That's why I either need help
    > > with renameTo or a better method.

    >
    > > By the way, res already exists (with contents and 1kb in size), and
    > > was opened using:
    > > File res = new File(resultsLocation.concat("/results.txt"));

    >
    > > I'm on a Windows XP by the way, if that matters...

    >
    > Try to use java.io.File.exists() method to ensure that source file
    > exists and target does not.
    > I am not not sure about XP, but maybe there is problem with file
    > separator character: '/'.
    >
    > Przemek


    Meh, still blank

    Have you used this before? If so, could you give me an example so I
    can compare what I'm doing to what works? Thanks.

  9. Default Re: Renaming and Moving Files:

    Danger_Duck wrote:

    > Meh, still blank
    >
    > Have you used this before? If so, could you give me an example so I
    > can compare what I'm doing to what works? Thanks.


    Run the following program few times and ****yze the results:

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;

    public class FileRenameExample {

    public static void main(String[] args) throws IOException {
    File f = new File("test-file.txt").getCanonicalPath();
    if (! f.exists()) {
    FileWriter fw = new FileWriter(f);
    fw.write("Ala ma kota");
    fw.close();
    System.out.println("created file " + f.);
    } else {
    System.out.println("file " + f.getCanonicalPath() + " exists");
    }

    File f2 = new File("test-file-renamed.txt");
    if (f.renameTo(f2)) {
    System.out.println("renamed to " + f2.getCanonicalPath());
    } else {
    System.out.println("can't rename to " + f2.getCanonicalPath());
    }
    }
    }


    piotr

  10. Default Re: Renaming and Moving Files:

    Danger_Duck wrote:

    > Meh, still blank
    >
    > Have you used this before? If so, could you give me an example so I
    > can compare what I'm doing to what works? Thanks.


    Run the following program few times and ****yze the results:

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;

    public class FileRename {

    public static void main(String[] args) throws IOException {
    File f = new File("test-file.txt").getCanonicalFile();
    if (! f.exists()) {
    FileWriter fw = new FileWriter(f);
    fw.write("Ala ma kota");
    fw.close();
    System.out.println("created file " + f);
    } else {
    System.out.println("file " + f + " exists");
    }

    File f2 = new File("test-file-renamed.txt").getCanonicalFile();
    if (f.renameTo(f2)) {
    System.out.println("renamed to " + f2);
    } else {
    System.out.println("can't rename to " + f2);
    }
    }
    }


    piotr

+ Reply to Thread
Page 1 of 2 1 2 LastLast