Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to rename a file or How to move a file to another dir) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java IO (Java File - How to rename a file or How to move a file to another dir)
FileDemo1.java
import java.io.File;

/*
 * public boolean renameTo(File dest)
 * 
 * Parameters: 
 * ----------
 * 
 * dest - The new abstract pathname for the named file
 */
public class FileDemo1
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/myfile.txt");
        /*
         * Renames the file denoted by this abstract
         * pathname.
         * 
         * Returns: true if and only if the renaming
         * succeeded; false otherwise
         */
        boolean success = file.renameTo(new File("D:/work/java.txt"));
        System.out.println("success = " + success);
    }

}
Output
success = true
FileDemo2.java
import java.io.File;

/*
 * public boolean renameTo(File dest)
 * 
 * Parameters: 
 * ----------
 * 
 * dest - The new abstract pathname for the named file
 */
public class FileDemo2
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/myfile.txt");
        /*
         * Renames the file denoted by this abstract
         * pathname.
         * 
         * Returns: true if and only if the renaming
         * succeeded; false otherwise
         */
        boolean success = file.renameTo(new File("D:/work/misc/java.txt"));
        System.out.println("success = " + success);
    }

}
Output
success = true
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment