0

I've been working on my final programming class project, and I am stuck right now, I have to create an inventary for a company. I use textpad to write the code and the icarnegie workbench, to put the classes on it and run it, so the thing is that i have this servlet and from there I call the class called Delete, this class has various methods, each of them deletes a file, something like this:

import java.io.*;
public class Delete{

String nombre;


public Delete(String n){
 nombre=n;
 }


public void deleteNombre(){

File objt = new File("C:/Inventario/"+nombre+"/nombre.txt");
objt.delete();

 }

public void deleteCodigo(){

File objt = new File("C:/Inventario/"+nombre+"/codigo.txt");
objt.delete();

 }

public void deletePrecio(){

File objt = new File("C:/Inventario/"+nombre+"/precio.txt");
objt.delete();

 }

public void deleteCantidad(){

File objt = new File("C:/Inventario/"+nombre+"/cantidad.txt");
objt.delete();

 }



 }

When I try to call this from the servlet, I can compile successfully, I don't get any errors. when I put this code on a main class and run it, in terminal, the files are deleted, but when I use this method, calling it from a servlet, it just doesn't happen. How can this happen and how can I fix it?

3 Answers 3

1

The files are most likely still open somewhere else in the code.

Sign up to request clarification or add additional context in comments.

3 Comments

oh oh , that could be it, because i search for them, so how can i close then? object.close(); wait not, that is not a method from the File Class
@david - close the readers/writers or streams you used on the file.
THANK U SOOOO MUCH, I totally forgot to use .close(); after reading a file in the Search and Show Function. lol
1

A good starting point would be to check whether the file exists before deleting it and check for the return value of that delete() call. Something like:

File f = new File(something);
if(f.exists()) {
  if(!f.delete()) {
    System.out.println("Deleting " + f + " failed");
  }
} else {
  System.out.println("The file " + f + " doesn't exist");
}

1 Comment

thanks for the advice already try that, the files does exist, i mean i create them from the system, in the system I can delete, insert, search and show everything. i use the File class for that.
0

Assuming that you've traced the code and these methods are actually getting called, this is possibly a permissions issue. Does the servlet container user have permission to access and alter those files?

That said, you probably wouldn't design a servlet application to mess around with the filesystem for non-binary data; typically you'd put this information in a database. It will avoid these permission issues, give you greater flexibility, and you wouldn't get into concurrent access issues (e.g. what happens if two users of your servlet try to access the file at the same time?)

2 Comments

yes, i mean the files are created in the system, they are not protected, the weird thing is that it used to work fine, but not anymore for some reason. i also tried in different computers but i get the same issue, i can inset, search, and show all the files, with different classes i created but the only thing i can't do and only works sometimes, is this. :( btw, thanks for replying fast :) u rock
i know, but I'm taking programming 1 and this is what i have to do. it is not real i mean is just a project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.