Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file delete...

 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aruna Devi V.P
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
srivatsan srinivasan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes you can use .delete() from a servlet, so what was the problem?
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic