Forums Register Login

file delete...

+Pie Number of slices to send: Send
hi all,
i have a servlet which writes something to a text file(say filename.txt).i want that whenever the servlet is executed again then it should delete the already existing filename.txt and create a new one.how can this be done..please help..its urgent..(i tried File.delete() but it throws some weird exception and doesnt let me delete the file)...
PS-can file deletion be done from an applet?
thanks a ton
regards
karan
+Pie Number of slices to send: Send
hi karan,
put your logic as follows.
check whether file exists f.exists() and check whether file is a directory if yes delete recursively or else f.delete()
public void deletedb(String h)
{
File dbFile = null;
try
{
dbFile=new File (dbName);
if (dbFile.isDirectory())
{
String[] dbFiles = dbFile.list();
for (int i = 0; i < dbFiles.length; i++)
{
String fullName =dbFile.getAbsolutePath()+"\\"+dbFiles[i];
File tmp =new File(fullName);
if(tmp.isDirectory() && tmp.exists())
{
deleteOldDb(fullName);//same function called recursively
}
else
{
try
{
tmp.delete();
}
catch(SecurityException ss)
{
ss.printStackTrace();
}
catch(Exception ff)
{
ff.printStackTrace();
}
}
}//end of for
result = dbFile.delete();
}
else
result = dbFile.delete();
}
catch(Exception kk)
{
}

hope this helps u
Regards,
Aruna
+Pie Number of slices to send: Send
hi karan,
I have faced a similar problem .you can not delete a file which is refrenced by any server.even as aruna suggested is not possible i have tried that.what i did is that i created a new file object with the same file name.so now the file is empty .so any text is return from the scratch.
+Pie Number of slices to send: Send
Well, yes you can use .delete() from a servlet, so what was the problem?
+Pie Number of slices to send: Send
hi aruna and srinivas,
i worte the following code for deleting the file in my servlet as aruna said and it gave me the following exception in my applet.
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");

File name = new File("/","node.txt");
if(name.exists() && name.isFile())
{
name .delete();
}

File name1 = new File("/","link.txt");
if(name1.exists() && name1.isFile())
{
name1 .delete();
}

File name2 = new File("/","incident.txt");
if(name2.exists() && name2.isFile())
{
name2 .delete();
}


FileWriter fw = new FileWriter("dir path" + "node.txt", true);
BufferedWriter bw = new BufferedWriter(fw);

FileWriter fw1 = new FileWriter("dir path" + "link.txt", true);
BufferedWriter bw1 = new BufferedWriter(fw1);

FileWriter fw2 = new FileWriter("dir path" + "incident.txt", true);
BufferedWriter bw2 = new BufferedWriter(fw2);
now i write the 3 files
}
this is the exception
java.lang.NumberFormatException: multiple points
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1036)
at java.lang.Double.valueOf(Double.java:168)
at Simple.drawPoint_database(Simple.java:461)
at Simple.paint(Simple.java:715)
at javax.swing.JApplet.update(JApplet.java:133)
at sun.awt.RepaintArea.update(RepaintArea.java:337)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:197)
at java.awt.Component.dispatchEventImpl(Component.java:2665)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:103)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
simu
java.lang.NumberFormatException: multiple points
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1036)
at java.lang.Double.valueOf(Double.java:168)
at Simple.drawlink_database(Simple.java:536)
at Simple.paint(Simple.java:731)
at javax.swing.JApplet.update(JApplet.java:133)
at sun.awt.RepaintArea.update(RepaintArea.java:337)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:197)
at java.awt.Component.dispatchEventImpl(Component.java:2665)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:103)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
basically wheni execute my applet then it communicates with the servlet which in turn generates 3 output files which are then read byu the applet.but i want them to be new files when the applet reads them.but as i menitioned above.it is not working.
+Pie Number of slices to send: Send
hey srinivas,
can u take a look at my code attached above and provide me with a code of what u did to take care of the problem..please..it would be a great help,really..
regards
karan
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1458 times.
Similar Threads
help with file deletion..please help..
help with file deletion...
Delete a file in java
how to delete content from a text file?
help with file deletion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:14:27.