java file.exists() function

hi,

i am trying to adapt this code into my project, but i find that it doesn't work the way i think it would work. here is the example i am following:

import java.io.File;

void setup()
{
   File f = null;
   boolean bool = false;

   try{
      // create new files
      f = new File("test.txt");

      // create new file in the system
      f.createNewFile();

      // tests if file exists
      bool = f.exists();

      // prints
      System.out.println("File exists: " + bool);

      // if(bool == true)
      // {
      //    // delete() invoked
      //    f.delete();
      //    System.out.println("delete() invoked");
      // }

      // // tests if file exists
      // bool = f.exists();

      // // prints
      // System.out.print("File exists: " + bool);

   }catch(Exception e){
      // if any error occurs
      e.printStackTrace();
   }
}

void draw()
{
}

this code works, but shouldn't it add a file named "test.txt" into the project folder? i think it adds it somewhere, because if i change the name of the file in the code to say "boom.txt," the code comes back false if f.createNewFile() is disabled. (the java docs for createNewFile() — "Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.") it doesn't create a new file.

also if i change "f = new File("test.txt");" to "f = new File(dataPath("test.txt"_);" i receive the following:

java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1006)
    at existsTest.setup(existsTest.java:31)
    at processing.core.PApplet.handleDraw(PApplet.java:2281)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:237)
    at processing.core.PApplet.run(PApplet.java:2177)
    at java.lang.Thread.run(Thread.java:744)

does anyone know what's going on?

essentially, i would like the user to add the name of a file into a textbox. if it matches the name of a file in the folder, it reads in its contents via loadstrings(). it works if i hardcode it, but it can't be hardcoded because the content in each of the files may not be the same.

basically, the problem i am having is that even if the code entered into the textbox matches the name of a file in the folder, it still returns false. this is my actual code:

if(theControlEvent.isFrom("messageFilename"))
  { 
    File filenameEntered = null;
    fileExists = false;

    try
    {
      println("in"); // this prints to the console
      filenameEntered = new File(dataPath("savedMessage/messages/" + cp5.get(Textfield.class, "messageFile").getText() + ".txt"));
      filenameEntered.createNewFile();

      fileExists = filenameEntered.exists();

      println("filenameEntered: " + filenameEntered); //this doesn't print to the console
      println("fileExists: " + fileExists); //this doesn't print to the console

      if(fileExists == true)
        messageLines = loadStrings("savedMessage/messages/" + cp5.get(Textfield.class, "messageFile").getText() + ".txt");
    } 
      catch(Exception e) //this is always executed even if the file exists
      {
        println("doesn't exist");
        messageLines = loadStrings("savedFiles/empty.txt");
        cp5.getController("messageToggle").setValue(0);
        cp5.get(Textfield.class, "messageFilename").clear();
      }

      println("messageLines.length: " + messageLines.length);
  }
Tagged:

Answers

  • I don't know what your goal is but can't you use createWriter? http://processing.org/reference/createWriter_.html

  • edited April 2014

    hi danzou, (i hope i'm not the only one who thinks that's funny)

    i tried that first, but i still got the same result where the file wasn't recognized. actually, i was just about to delete this thread, but i saw that you answered.

    i went another route: 1. checked to see if the entered text matches a filename in the folder 2. if yes, set a boolean flag and do something. 3. if no and flag is false, do something else.

    it seems to work for the moment.

    cheers,

    destro.

  • Answer ✓

    new File("test.txt");
    this code works, but shouldn't it add a file named "test.txt" into the project folder?

    No. This is pure Java code, and Java has no concept of "project folder". The test.txt file will be created in a (semi-)random place, depending on your system... If you do saveStrings(), it is a Processing function, that knows the context of the sketch. But pure Java classes must be instructed where the sketch is, like using dataPath(), like you did. Or sketchPath(). dataPath() can fail if you don't have a data folder in your sketch folder...

  • i see.

    thanks, philho. even though i solved my problem, i was hoping someone would explain that.

    cheers.

Sign In or Register to comment.