Synchronized between Windows File and Java
Hi' could anyone help me..i have problem like this :
i have folder namedjavatutorial in my C:\ , i try rename that folder name intomyjavatutorial and read the new name (both operation rename and read using my java program)
this is my code :
File x =new File("C:\\javatutorial");
x.renameTo(new File("C:\\myjavatutorial"));
System.out.println(x.getName());
why java always print the old folder name?
if i see it in windows explorer the folder name already change. Why Java cannot synchronized between the new name and the old name? what i want java can print the new folder name...how to implement this?
i hope my question easy to understand...
thanks a lot...
Hi,A File object doesn't need to reference a file or folder at all. calling renameTo does only mean that you want to try to rename a folder or a file to a new name. It does not say anything about changing the file object itself.Kaj
Also as stated in the java.io.File class documentation:
"Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change."
Which mean in your case that x will allways have the same path C:\javatutorial.
To keep a reference of the new file it would have been better to do:File x = new File("C:\\javatutorial");
File y = new File("C:\\myjavatutorial");
x.renameTo(y);
System.out.println("old:"+x.getName());
System.out.println("new:"+y.getName());
Regards
thanks a lot for the answer...but i think you wrong :
>> Which mean in your case that x will allways have the same path C:\javatutorial.
i think x have different path with C:\javatutorial, cos i try to compare it like this, i put two folder under javatutorial, named a and b. and i change my code like this :
public myProgram()
{
File x = new File("C:\\javatutorial");
x.renameTo(new File("C:\\myjavatutorial"));
File[] file = x.listFiles();
for (int i = 0; i < file.length; i++)
{
System.out.println(file[i]);
}
}
if between x and C:\javatutorial have the same path, it will print :
C:\javatutorial\a
C:\javatutorial\b
but the fact java doesn't print that.
if i change into like this :
public myProgram()
{
File x = new File("C:\\javatutorial");
//x.renameTo(new File("C:\\myjavatutorial"));
File[] file = x.listFiles();
for (int i = 0; i < file.length; i++)
{
System.out.println(file[i]);
}
}
it will print :
C:\javatutorial\a
C:\javatutorial\b
so, i think between x and C:\javatutorial have different path..
> so, i think between x and C:\javatutorial have> different path..You are wrong. You are still mixing a File instance with an actual file or folder.kaj
this is your code :
File x = new File("C:\\javatutorial");
File y = new File("C:\\myjavatutorial");
x.renameTo(y);
System.out.println("old:"+x.getName());
System.out.println("new:"+y.getName());
between x and y have different path, if you try to print all the file under directory of x, it can't...
>> You are wrong. You are still mixing a File instance with an actual file or folder.so how to implement that? if i already change the folder name and i want to print all files under new directory name...thanks...
> public myProgram()
> {
>File x = new File("C:\\javatutorial");
>x.renameTo(new File("C:\\myjavatutorial"));
>
>File[] file = x.listFiles();
>
>for (int i = 0; i < file.length; i++)
>{
> System.out.println(file[i]);
>}
> }
>
>
> if between x and C:\javatutorial have the same path, it will print :
>
> C:\javatutorial\a
> C:\javatutorial\b
No it won't because it won't compile.
This will:import java.io.File;
public class RenameEg {
public static void main(String[] args) {
File x = new File("C:\\javatutorial");
x.renameTo(new File("C:\\myjavatutorial"));
File[] file = x.listFiles();
for (int i = 0; i < file.length; i++) {
System.out.println(file[i]);
}
}
}
With the following outputC:\>dir javatutorial
Volume in drive C is IBM_PRELOAD
Volume Serial Number is 400B-A170
Directory of C:\javatutorial
28/09/2006 07:06 p.m.<DIR> .
28/09/2006 07:06 p.m.<DIR> ..
28/09/2006 07:06 p.m.<DIR> a
28/09/2006 07:06 p.m.<DIR> b
0 File(s) 0 bytes
4 Dir(s)6,705,012,736 bytes free
C:\>javac -cp . RenameEg.java
C:\>java -cp . RenameEg
Exception in thread "main" java.lang.NullPointerException
at RenameEg.main(RenameEg.java:10)
C:\>dir javatutorial
Volume in drive C is IBM_PRELOAD
Volume Serial Number is 400B-A170
Directory of C:\
File Not FoundYou get the null pointer exception because listFiles() returns null. It
returns null becuse the folder had its name changed.
To list the files of the renamed folder, you will have to create a new File
based on the new (changed) name.
> >> You are wrong. You are still mixing a File
> instance with an actual file or folder.
>
> so how to implement that? if i already change the
> folder name and i want to print all files under new
> directory name...thanks...
See reply #2.
Kaj
>> No it won't because it won't compile.
Says who it wont compile? i think u need to know first what is constructor?
>> You get the null pointer exception because listFiles() returns null. It
>> returns null becuse the folder had its name changed.
>> To list the files of the renamed folder, you will have to create a new >> File based on the new (changed) name.
do you mean that i must reference to another variable? so i cannot using x ? thats very not good solution...
> do you mean that i must reference to another> variable? so i cannot using x ? thats very not good> solution...What's so hard to understand? Just read reply #2, and assing y to x after you have renamed the file. What's the problem?
>> See reply #2.i already understand about that, but i don't find any solution about that, did u already try it? or u just read the description in API? can i see your code?
> >> See reply #2.
>
> i already understand about that, but i don't find any
> solution about that, did u already try it? or u just
> read the description in API? can i see your code?
I really don't understand what the problem is.
File x = new File("C:\\javatutorial");
File y = new File("C:\\myjavatutorial");
if (x.renameTo(y)) {
x = y;
}
//Do whatever you want..
>> assing y to x after you have renamed the file.i just wont to be sure that is only way to do that? if this is only way to implement that, i already know...
> >> assing y to x after you have renamed the file.> > i just wont to be sure that is only way to do that?> if this is only way to implement that, i already> know...Why don't you believe us?
From the API docs:java.io.FileAn abstract representation of file and directory pathnames.
> Says who it wont compile?My big friend, javac.> do you mean that i must reference to another variable?No, no other variable is needed. But whatever variable is used, its value must be a reference to another File.
>> Why don't you believe us?
no man, it's not like that, i do believe in all of you, but i just want to make sure about that, cos this is the basic operation that i need to very very understand...for me this is important way..thanks a lot man...you're the best...
sorry about that...
> i just want to make sure about that, cos
> this is the basic operation that i need to very very
> understand...
You were given all information that you needed in post #2. jfbriere quoted the javadoc:
"Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change."
kajbja at 2007-7-21 11:23:25 >

(...and I told you many times to read reply #2)
kajbja at 2007-7-21 11:23:25 >

> Says who it wont compile?
public class myProgram
{
public myProgram()
{
File x = new File("C:\\javatutorial");
x.renameTo(new File("C:\\myjavatutorial"));
File[] file = x.listFiles();
for (int i = 0; i < file.length; i++)
{
System.out.println(file[i]);
}
}
public static void main(String[] args)
{
new myProgram();
}
}
i think you have an error in your compiler, javac returns 0 errors but when exectue got Exception..
>> > No, no other variable is needed. But whatever variable is
>> used, its value must be a reference to another File.
thanks man, i know it now..
thanks..
>> (...and I told you many times to read reply #2)thanks a lot..
> i think you have an error in your compiler, javac returns 0 errors
Fair enough! (I had considered this possibility, but didn't want to insult you
by suggesting that you would name a class like this.)
> but when exectue got Exception..
And that exception, and the reason for it were precisely what I was drawing
your attention to in my post.
Anyway, I'm glad you're happier now with the idea that Files are immutable,
and represent file and directory names rather than files themselves.
>> Fair enough! (I had considered this possibility, but didn't want to insult you
by suggesting that you would name a class like this.)
Yeah Right!! just say it that u don't know..thats fine..oke...don't be shy..
u need to learn more OOP...
Anyway, i'm happier too now..
thanks...