Results 1 to 7 of 7
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default Basic File Question

    I just want to rename this file and display it's new name. Whenever I run it, I just put in like "hey" and "hi" for command-line arguments (I just want it to work for learning purposes). It displays the old name and then the old name again instead of the new name.
    Java Code:
    import java.io.*;
    public class FileDemo {
    
    	public static void main(String args[])
    	{
    		try {
    			File file = new File(args[0]);
    			System.out.println("File's name is : " + file.getName());
    			File newName = new File(args[1]);
    			file.renameTo(newName);
    			System.out.println("File's new name is : " + file.getName());
    		}
    		catch(Exception e) {e.printStackTrace();}
    		
    	}
    	
    	
    	
    	
    }
    Why isn't it displaying the new name?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Check the return value of renameTo. It is probably false.

  3. #3
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    You're right, it is returning false. Why isn't it returning true?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Because it's failing to rename the file.

  5. #5
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    Well I get that -_-. Why is it failing? Is it a coding error on my part? If so, where at? Or is it maybe that I need to actually put in a real file name instead of test names like "hey" and "hi"?

  6. #6
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    I know it is a really noob question, but its kinda hard to move on without an answer. Any help is appreciated.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Well, the original file has to exist in order to be successfully renamed. Then, you have to have permission and access to do the actual rename.

Similar Threads

  1. Basic Netbeans question
    By McChill in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 10:22 PM
  2. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 07:09 PM
  3. Basic MouseListener Question
    By jshailes in forum AWT / Swing
    Replies: 9
    Last Post: 01-15-2009, 08:58 AM
  4. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 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
  •