• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why return false when renaming a file?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The program do these things:
1.read the source file line by line;
2.modify the string;
3.write the string to a temp file;
3.rename the temp to the source file.
But I found sometimes the method renameTo() would return false. Would you pls tell me why? What is the mechanism of file's renaming? When it return false and when do true? Thank you !
The code lists below:
try {
//turn the source file f to the Reader
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(f)));
//turn the temp file temp to the writer
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(temp))),true);
String s = new String();
while ( (s = in.readLine()) != null) {

s = modifyString(s);//invoke my own method
//to modify string
out.println(s);
}
in.close();
out.close();
System.out.println(temp.renameTo(f));//rename temp file to the
//original file
System.out.println(temp.getPath());
System.out.println(f.getPath());
}
catch (FileNotFoundException fnfe) {}
catch (IOException e) {}
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, the J2SE API documentation is usually used to find out the meaning of return values. You can find it at: http://java.sun.com/j2se/1.4.2/docs/api/index.html.
 
Richard Yi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the extract from API. But I can't find out the reason.
renameTo
public boolean renameTo(File dest)Renames the file denoted by this abstract pathname.
Whether or not this method can move a file from one filesystem to another is platform-dependent. The return value should always be checked to make sure that the rename operation was successful.

Parameters:
dest - The new abstract pathname for the named file
Returns:
true if and only if the renaming succeeded; false otherwise
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that this is the same problem I had about two months ago. Try deleting the file before you rename it. I think that it returns false when the file already exists.
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Under Windows, if you try to rename X to Y, and Y already exists, the rename will fail. Under Unix, if you try to rename X to Y, and Y already exists, the rename will succeed. I believe that under Unix, rename uses the mv command, which will overwrite the target if it exists.
Thus the renameTo method will return different boolean values, depending on the platform.
The renameTo method will also fail for a multitude of other reasons, such as: the target file is locked, you do not have appropriate write access, or you have specified an invalid file name.
-Jeff-
[ March 26, 2004: Message edited by: Jeff Langr ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic