problem in deleting and renaming?

i have a text file in which i have to change some values in it.

so i tried using this code..

asciiFile =new File(FileName);

TempFile =new File(asciiFile.getParent(),"Temp1.xml");

BufferedReader buff =new BufferedReader(new FileReader(asciiFile));

BufferedWriter bw =new BufferedWriter(new FileWriter(TempFile));

for(String line =""; (line = buff.readLine()) !=null;)

if(line.contains("<equation format=\"inline\">"))

{

for(int vv = 0; vv < ActualEquationToBePasted.size(); vv++)

{

String ff = line.replace(EquationToBeExceuted.elementAt(vv).toString(), ActualEquationToBePasted.elementAt(vv).toString());

line = ff;

}

bw.write(line);

bw.write("\n");

// System.out.println(line);

}else

{

bw.write(line);

bw.write("\n");

// System.out.println(line);

}

buff.close();

bw.close();

File org =new File(FileName);

System.out.println(asciiFile.delete());

TempFile.renameTo(org);

Actually i created the temp file and writing the modified file. and i try to rename that file to the original filename b4 that i am deleting the orgn. file. but its not deleting. it is reutrning false. can anyoine help out in this.

[2069 byte] By [DineshGovindana] at [2007-10-2 5:49:12]
# 1
Have you do a 'close' command, of the buffer and the file after manipulate the file?: )
JosepBravoa at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 2

you created a new reference to that file so you cannot delete or rename...

File org = new File(FileName);

System.out.println(asciiFile.delete());

TempFile.renameTo(org);

to get it to work, don't create the new file...

simply call delete() the old file then pass that File object to the renameTo() method...

asciiFile.delete();

TempFile.renameTo(asciiFile);

and that will take care of the issue...

and you are done and you can dispose of the file objects as you wish...

have fun... :-)

- MaxxDmg...

- ' He who never sleeps... '

MaxxDmga at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 3

yeah i tried like thisa.

System.out.println(asciiFile.delete());

TempFile.renameTo(asciiFile);

but didnt work out..

i have inserterd

System.gc();

its working fine. could anyone tell me is this kind of calling garbage collector explicitly gives any problem.

DineshGovindana at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 4

> i have inserterd

> System.gc();

>

> its working fine. could anyone tell me is this kind

> of calling garbage collector explicitly gives any

> problem.

Explicit call to garbage collector doesn't guarantee GC. Also, it may not be necessarily helpful.

http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997476

http://java.sun.com/developer/technicalArticles/ALT/RefObj/

http://www-106.ibm.com/developerworks/java/library/j-jtp01274.html

aniseeda at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 5
i think System.gc() didnt help me out..even i tried ur way of code..like System.out.println(asciiFile.delete());TempFile.renameTo(asciiFile);but still it is returning false.
DineshGovindana at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 6

works here...

import java.io.*;

public class FileDelRen {

public static void main(String[] xyz){

String fName = "file1.txt";

String tName = "temp.tmp";

File file = new File(fName);

File temp = new File(file.getParent(),tName);

try{

BufferedReader br = new BufferedReader(new FileReader(file));

BufferedWriter bw = new BufferedWriter(new FileWriter(temp));

String data = null;

while( (data = br.readLine()) != null ){

bw.write(data,0,data.length());

}

br.close();

bw.close();

br = null;

bw = null;

file.delete();

System.out.println( " File renamed : " + temp.renameTo(file));

}catch(FileNotFoundException fnfe){

fnfe.printStackTrace();

}catch(IOException ioe){

ioe.printStackTrace();

}

}

}

btw, i added the br = null; and bw = null; to ensure there are not references to the File objects in question...

- MaxxDmg...

- ' He who never sleeps... '

MaxxDmga at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 7
no ur program is doing fine.. even i have done this kind of jobs. but the problem is i have used different method to process the same fiel for different purpose. any way thnx for your kind help. i am going thru abt the System.gc().
DineshGovindana at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 8

a call to System.gc() does not guarantee that the GC will run, it more like a request...

if you are referencing the file in other methods and classes and such, you could simply encapsulate the File object refering to the file into a class that also include the read and write capabilities with a renaming method so that only one object has a reference to the File object... especially if you are using the same code to do the reading and writing using temp files...

just a thought... :-)

- MaxxDmg...

- ' He who never sleeps... '

MaxxDmga at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...
# 9
sure i discuss abt this.. with u..
DineshGovindana at 2007-7-16 1:58:41 > top of Java-index,Java Essentials,Java Programming...