Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Problems changing file name

    Hi guys,

    Im starting to learn java and I'm having a little trouble remaming a file. My program creates the file on the desktop and renames it but when I try to print the name of the file in the console its come up with the original file name.

    Any help would be great!! thanks!


    import java.util.Scanner;
    import java.io.*;
    import java.util.*;
    class testing{
    public static void main(String args[]){
    Scanner in = new Scanner(System.in);


    File newFile= new File("C:\\Users\\Public\\Desktop\\bren.txt"); //creates a new instance of file
    try{
    newFile.createNewFile(); //actually creates the file
    newFile.renameTo(new File("C:\\Users\\Public\\Desktop\\john.txt")); //renames file
    }

    catch(Exception e){

    }
    System.out.println(newFile.getName());



    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    newFile= new File("C:\\Users\\Public\\Desktop\\bren.txt")
    This is the value of the newFile object. Is that what you see when you print?

    What is printed after the rename if you call the newFile.exists() method?

  3. #3
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Hey thanks for the reply

    It just prints bren.txt . The actual file on my desktop has changed name to john.txt tho. If i print newFile.exists() it prints true and still prints bren.txt .

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    When I executed the code with exists it returned false:
    B name=BeforeRename.txt exists=true
    A name=BeforeRename.txt exists=false

  5. #5
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    I could have done something wrong. I am a total beginner. Only been studying java and programming for 2 months.

    After I created the file and renamed it i used this command,

    System.out.prinln(newFile.exists()); and it returned true.

    How come its renaming the file on the pc but no in the console??

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Look in the directory and see what files exist?
    Delete both of the test files and execute the program.
    Also capture and display the results of the create and the rename method calls so you know if they succeeded or failed.

  7. #7
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Ok did all that. Create file is comin back as true and so is rename. And the file is being created on my desktop and being renamed to john.txt.
    Is it me or is this a bit strange??
    Attached Thumbnails Attached Thumbnails Problems changing file name-pic2.jpg  
    Last edited by mrbrendano; 07-14-2011 at 05:55 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Create file is comin back as true and so is rename. And the file is being created on my desktop and being renamed to john.txt.
    Delete the files again and execute the code.
    Please show the printed output from your program.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  9. #9
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    can u make out the pic?? its a bit small. oh hey, thanks for the help too. I appreciate it

  10. #10
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.util.*;
    class testing{
    	public static void main(String args[]){
    		Scanner in = new Scanner(System.in);
    
    
    		File newFile= new File("C:\\Users\\Bren\\Desktop\\bren.txt");	//creates a new instance of file
    		try{
    			boolean file=newFile.createNewFile();	//actually creates the file
    			System.out.println(file);
    			boolean file2=newFile.renameTo(new File("C:\\Users\\Bren\\Desktop\\john.txt")); //renames file
    			System.out.println(file2);
    		}
    
    		catch(Exception e){
    
    		}
    		System.out.println(newFile.getName());
    
    
    
    	}
    
    }



    output
    true
    true
    bren.txt

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Can you put labels on the printout? What does it mean when you print "output? or "true"?
    I can't easily tell looking at the printout what each line means.

    For example: System.out.println("rename returned=" + file2);


    Where is the printed value for the exists() method?

    Print out the value of exsits before and after the try block

  12. #12
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Sorry man!!


    New File Created=true
    File Renamed=true
    File Name:bren.txt

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    You missed this:

    Where is the printed value for the exists() method?

    Print out the value of exsits before and after the try block


    Be sure to erase all the test files before executing the code.

  14. #14
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    God haha sorry again!! Head isnt workin right today!!

    new code
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.util.*;
    class testing{
    	public static void main(String args[]){
    		Scanner in = new Scanner(System.in);
    
    
    		File newFile= new File("C:\\Users\\Bren\\Desktop\\bren.txt");	//creates a new instance of file
    		boolean exists=newFile.exists();
    		System.out.println("File Exist before try catch:"+exists);
    		try{
    			boolean file=newFile.createNewFile();	//actually creates the file
    			System.out.println("New File Created="+file);
    			boolean file2=newFile.renameTo(new File("C:\\Users\\Bren\\Desktop\\john.txt")); //renames file
    			System.out.println("File Renamed="+file2);
    		}
    
    		catch(Exception e){
    
    		}
    		System.out.println("File Exist after try catch:"+exists);
    		System.out.println("File Name:"+newFile.getName());
    
    
    			
    	}
    
    }

    new output

    File Exist before try catch:false
    New File Created=true
    File Renamed=true
    File Exist after try catch:false
    File Name:bren.txt


    Seems to be a problem there alright!

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    File Exist after try catch:false
    What is the problem? The output says that the file does NOT exist. Is that correct?

    Or are you confusing the contents of a File object with the existence of a file?
    Look at how you created the file. Did you create a File object? Did that file exist before you called createNewFile?
    Then look at the renameTo method call. Did you create a File object for a file that did not exist?

  16. #16
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    im not sure what ive done to be honest with ya!! I just thought when i call the file name at the very end it should be called john.txt instead of bren.txt considering thats what the file was renamed to and thats what the file actually on my desktop was called.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    It appears that the contents of the File object doesn't change.

  18. #18
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    so i cant change the output to the console?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    can't change the output to the console?
    Can you explain what you mean here?
    On many command prompt/console windows, once you print something you can't change what was written.

  20. #20
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    No no. The program create a new file called bren.txt.
    Then changes the file name to john.txt
    then I print the name of the file. and even tho ive the name changed to john.txt its still printing the old file name, bren.txt. I would have imagined it would print the new file name.

    I know I cant change whats printed in the console. its just printing the old file name instead of the new file name

Page 1 of 2 12 LastLast

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  3. Changing Existing Zip file using java
    By narayanan.1985 in forum Advanced Java
    Replies: 1
    Last Post: 09-30-2009, 08:27 PM
  4. Replies: 1
    Last Post: 10-16-2008, 09:56 PM
  5. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 10:10 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
  •