[Processing 3] Unable to Delete Files in /data through Java.io.File

This is the code… I think the issue is how I am using Java.io.File but I have no idea.

String str = new Integer(i).toString();
        String fileName1 = dataPath(str+".csv");
        File f1 = new File(fileName1);
        String fileName2 = dataPath("arr"+str+".JSON");
        File f2 = new File(fileName2);
        String fileName3 = dataPath("player"+str+".JSON");
        File f3 = new File(fileName3);
        if (f1.exists()){
          f1.delete();
          f2.delete();
          f3.delete();
       }

Even without

  if (f1.exists()){
         
       }

It does not do anything

If I check if it deleted (without exists either) , through

if (f1.delete()){
}

It still comes out false.

Anyone’s contribution is highly appreciated!

did you suggest/check that

  • the directory /data/ exists ( or expect that it is created? )
  • the file exists already at /data/filename
    ( or that it is created by that code? )

– but your code does not create anything here
( win7 64bit / processing 3.5.3. )
otherwise,
if i create the path and file manually,
the used file exists and delete functions work just fine.


next time you paste code please

just play/post for one fix test filename
( and not with 3 created filenames )

and make it a short but RUNnable code


here the full way, with directory and file creation
what is somehow useless as processing can not use existing empty files for append?
more a academic exercise

String fileName = "test.txt", fullfileName, fullpathName;
File newp, newf;
boolean pathok, fileok;

void setup() {
  fullpathName = dataPath("");
  newp = new File(fullpathName);
  pathok = newp.exists();
  println("path : "+fullpathName+" found: "+pathok);
  if ( !pathok ) { 
    newp.mkdir();
    println("dir created");
  }
  fullfileName = dataPath(fileName);
  newf = new File(fullfileName);
  fileok = newf.exists();
  println("file : "+fullfileName+" found: "+fileok);
  if ( !fileok ) 
  try { 
    newf.createNewFile();
    println("file created");
  }
  catch(IOException e) {
    throw new IllegalStateException(e);
  }
}


void draw() {
}

void keyPressed() {
  if ( key =='d' ) {
    if ( newf.exists() ) {
      print("file exists,");
      newf.delete();
      println(" now deleted!");
    }
  }
}

the program structure allows you to check ( by file manager )
if path and file exists,
and then press ‘d’
and check again.

Thank you for the help, I agree that I should’ve only used one filename for the question.