views:

91

answers:

3

The idea is to take one single file, but I don't want to list all the files, I have the address of the specified folder, but not the name.

Basically I want

findFileInFolder(String folderName) --- this method returns a random filename or the oldest file created on the folder

Does anybody ever tryied doing this? any Ideas to avoid listing all the files in an array and then taking the firstone?


Added:

Just in case I wasn't clear (I'm really sorry for my english, please forgive me if I sound prepotent or agressive is really not in my intentions) the file is not selected by a human, it's selected by the machine not even asking or showing the file except for the method that returns a string with the FileName

String findFileInFolder(String folderName)

Like I comment is for ussage of ram and proccesor, because this is a secondary proccess and not the primary of the proyect, so if I have to read over a thousand files it will considerably reduce the performance of my proyect :(

Thanks ;)


New add, the program is running on different computers so if I could just access the directory not "thinking" about reading wich file it would be great =D


Hopefully last add, sorry for bothering you guys :)

for what I read on the answers there is no way, so my question is, what good alternatives instead of doing an array would you think? my idea was making an index in a textfile and taking only the first line.

A: 

You can have a look on FileFilter class and on the public File[] listFiles(FileFilter filter)

Using this method you will be able to filter files according to their last modification date for example.

On a side note, why do you want to avoid to list all the files, for performances concerns ?

Manuel Selva
A: 

There is no way of doing this in current versions of java other than listing all files and selecting what you need. If you can use java 7, there is a FileVisitor class that can walk a folder tree without listing all of the files.

tulskiy
I can't =(, the other chooice that I do have is to make an index of files and removing one by one, but it wasn't my idea, I would like not having this magic file because it kills my performance too :-S
Saikios
Have you ran any benchmarks about File.list() performance? It is a native method that returns only an array of file names, so thousand strings is not that much.
tulskiy
Thousands was just to say something, it's probably going to be lot's more and different proccess and computers running at the same time, I will add it in the info
Saikios
A: 

I decidee to use this code, is not exactly what I wanted but it works for now.

  public static String getFileToCrawl(String directory){
      File dir = new File(directory);

      String[] children = dir.list();
      if (children == null) {
          return "";
      } else {
          int i=0;
          String filename = children[i];
          while (i<children.length && !filename.contains(".txt")){
              i++;
              filename = children[i];
          }
          return filename;
      }

  }

if anyone like it or know a way to improve this code it's really welcome ;) if you want to use it feel free :D

Saikios