Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can not rename my File

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Bangalore
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can not rename my File

    hello everybody i am new to java plz help me out...

    I have written the following code in order to rename the file cfile.txt after adding some data in that file but renaming can't be happened.

    import java.io.*;
    public class File3
    {
    public static void main(String [] args)throws IOException
    {
    File b=new File("b");
    b.mkdir();
    File bfile=new File(b,"bfile.txt");
    bfile.createNewFile();
    File cfile=new File(b,"cfile.txt");
    cfile.createNewFile();
    FileWriter cwrite =new FileWriter(cfile);
    cwrite.write("this is the secod file of directory");
    cwrite.flush();
    File abcd =new File(b,"abcd.txt");
    System.out.println(cfile.renameTo(abcd));
    }
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Bangalore
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can not rename my File

    i got the answer i am trying renaming the file which has some data in the file.But according to java norms the renameTo() can only rename file which is empty.
    So if i changed System.out.println(cfile.renameTo(abcd)); to System.out.println(bfile.renameTo(abcd)); it will fine...

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can not rename my File

    What happens instead? And why are you simply throwing that Exception instead of catching it like you should be?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to rename a file in your computer in Java?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: December 24th, 2011, 12:05 PM
  2. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  3. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  4. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  5. How to rename a file in your computer in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:52 AM

Tags for this Thread