File Name reader problem

edited May 2015 in How To...

hi,

I am trying to make a data store and I want to ask the user the question if he wants to use an existing file or he wants to create a new one, for that I have to read the filenames and print him the existing ones, and I don't know how to do that, I already had a look at another contribution but that didn't help me (I am not the best programmer)

Answers

  • import java.io.File;

    File folder; String [] filenames;

    void setup() { java.io.File folder = new java.io.File(dataPath(" ")); }

    void draw() { filenames = folder.list(); println(filenames.length + ".properties.ser");

    for(int i = 0; i <= filenames.length; i++) { println(filenames[i]); } }

  • @Skyfreak== try this (but there are a lot of other ways); getName() is (for me) the solution::

        import java.io.File;
    
        String cheminData; //path towards folder
        String fichiers; // var for the names of the files
    
    
        void setup(){
          size(200,200);
    
          cheminData = dataPath("");
          getFilesFromFolder(cheminData);
    
        }
    
        public void getFilesFromFolder(String folderPath) {
          int nbre = 0; // number of files inside the folder
    
            File dir = new File(folderPath); // the directory
            File[] files = dir.listFiles(); // list for the files inside
    
            String[] resultat = new String[files.length];//useful if you want to reuse
    
    
             for (int i = 0; i < files.length; i++) 
          {
    
           if (files[i].isFile()) 
           {
           fichiers = files[i].getName();
    
           resultat[i] = fichiers;
           System.out.println(fichiers);
              }
          }
          nbre = (files.length);
          System.out.println(nbre);
    
          }
    
  • I'll try it out thanks ;-)

  • okay, got a better idea but thanks

  • are you sure that your files are in the data folder of your sketch; i have tested my code & it runs...

  • ah, i see , error with copy & paste, i forgotten string[] resultat!!!! so===

    import java.io.File;
    
    String cheminData;
    String fichiers;
    String[] resultat;// forgotten!!!!
    
    
    void setup(){
      size(200,200);
    
      cheminData = dataPath("");
      getFilesFromFolder(cheminData);
    
    }
    
    public void getFilesFromFolder(String folderPath) {
      int nbre = 0;
        File dir = new File(folderPath);
        File[] files = dir.listFiles();
        resultat = new String[files.length];
    
    
         for (int i = 0; i < files.length; i++) 
      {
    
       if (files[i].isFile()) 
       {
       fichiers = files[i].getName();
       resultat[i] = fichiers;
       System.out.println(fichiers);
          }
      }
      nbre = (files.length);
    
      println(nbre);
    
      }
    
  • edited May 2015
    • We can directly get a File instance from "/data/" subfolder using dataFile() in place of dataPath()!
    • And File doesn't need to import. It's already done by Processing! :P

    String[] archives;
    
    void setup() {
      archives = getFilesFromDataFolder();
      printArray(archives);
      exit();
    }
    
    String[] getFilesFromDataFolder() {
      return dataFile("").list();
    }
    

    P.S.: If you also wanna filter by file extensions, pass an instance of FilenameFilter as argument for list():

  • @goToLoop== - thanks! i always imported File without thinking! - i dont know the printArray() method you use (kind of ArraysToString() i think?) - & cannot find it in reference

  • @goToLoop== ok; added in2.0: i used the 1.5.1 reference and found nothing of course best wishes!

  • P2 added printArray() b/c println() somehow lost its original way of displaying arrays.
    In other words, P2's printArray() behaves exactly as P1's println() for arrays! :-B

  • See How to format code and text, avoid posting code in screenshots, they are barely readable, cannot be searched nor copy & pasted in the PDE..

Sign In or Register to comment.