Results 1 to 20 of 23
Thread: Problems changing file name
- 07-14-2011, 02:12 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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());
}
}
- 07-14-2011, 03:16 PM #2
This is the value of the newFile object. Is that what you see when you print?newFile= new File("C:\\Users\\Public\\Desktop\\bren.txt")
What is printed after the rename if you call the newFile.exists() method?
- 07-14-2011, 04:37 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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 .
- 07-14-2011, 04:45 PM #4
When I executed the code with exists it returned false:
B name=BeforeRename.txt exists=true
A name=BeforeRename.txt exists=false
- 07-14-2011, 05:27 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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??
- 07-14-2011, 05:29 PM #6
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.
- 07-14-2011, 05:51 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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??Last edited by mrbrendano; 07-14-2011 at 05:55 PM.
- 07-14-2011, 05:56 PM #8
Delete the files again and execute the code.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.
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.
- 07-14-2011, 05:59 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
can u make out the pic?? its a bit small. oh hey, thanks for the help too. I appreciate it
- 07-14-2011, 06:02 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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
- 07-14-2011, 06:10 PM #11
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
- 07-14-2011, 06:21 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Sorry man!!
New File Created=true
File Renamed=true
File Name:bren.txt
- 07-14-2011, 06:22 PM #13
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.
- 07-14-2011, 06:27 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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!
- 07-14-2011, 06:33 PM #15
What is the problem? The output says that the file does NOT exist. Is that correct?File Exist after try catch:false
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?
- 07-14-2011, 07:15 PM #16
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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.
- 07-14-2011, 07:18 PM #17
It appears that the contents of the File object doesn't change.
- 07-14-2011, 07:24 PM #18
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
so i cant change the output to the console?
- 07-14-2011, 07:33 PM #19
Can you explain what you mean here?can't change the output to the console?
On many command prompt/console windows, once you print something you can't change what was written.
- 07-14-2011, 07:41 PM #20
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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
Similar Threads
-
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
Changing binding when changing the underlying model object
By ChrisNY in forum NetBeansReplies: 0Last Post: 08-14-2010, 10:09 AM -
Changing Existing Zip file using java
By narayanan.1985 in forum Advanced JavaReplies: 1Last Post: 09-30-2009, 08:27 PM -
Can we grant permission to applets with out changing java policy file?
By kuppi in forum Java AppletsReplies: 1Last Post: 10-16-2008, 09:56 PM -
Reading Binary File and Changing data
By janakiram.attuluri in forum Advanced JavaReplies: 1Last Post: 12-21-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks