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.
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... '
> 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
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... '
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... '