2

I'd like to create a file object as follows

File file = new File("MyFile-abcdfg.txt");

where the string between - and . is random and always changing. The length is also not the same.

I want to check the file.exist(), but the problem is I am not sure what will be the name of the file, as it keeps on changing.

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
user2256009
  • 169
  • 1
  • 2
  • 9
  • could you explain this a bit more, since this sounds really confusing. Maybe you want to create a `String` variable which you can set dynamicly? – SomeJavaGuy Jan 20 '16 at 16:29
  • Possible duplicate of [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string) – resueman Jan 20 '16 at 16:29
  • 1
    I smell some [XY problem](http://xyproblem.info/) – Aaron Jan 20 '16 at 16:32
  • If you want to check to see what file it is, I think you would want to do a brute force check based on your possible combinations...Not generate one randomly. Is this correct? – mattsap Jan 20 '16 at 16:38
  • If this is really all you know, then you're screwed. However, you can get a list of files in the directory (by creating a File object that references the directory and calling list() on it), and if you know something else about your file you can find it in the list -- maybe it's the newest, or its the only file there, or it's always increasing in alphanumeric value, or something. If you don't know anything else other than what you've told us then you'll not be able to solve this. – Software Engineer Jan 20 '16 at 16:48

5 Answers5

1

You can find the Possible solution over here.

List of files starting with a particular letter in java

Thanks

Community
  • 1
  • 1
Arun
  • 875
  • 3
  • 10
  • 17
0

You can create a String variable for example like this:

String dynamicPartOfFileName = "abcdfg";

If you want to you can replace the literal "abcdfg" by any other mechanism (such as generating a randomized String).

And use it as part of the filename like this:

File file = new File("MyFile-" + dynamicPartOfFileName + ".txt");

The +-operator will join the Strings together. Afterwards the new File()-constructor will use the joined String.

slartidan
  • 20,403
  • 15
  • 83
  • 131
0

You can use random numbers to randomly pick values from your possible file names.

Random rand = new Random();
int randomNumber = rand.nextInt(2); // 0-1.
String s1 = "-";
if(randomNumber == 0){
    s1 = "_";
}

int nameLength = rand.nextInt(100); //0-99
String characters = "";
String possibleCharacters = "abcdefg";
for(int i = 0; i < nameLength; i++){
    characters += possibleCharacters[rand.nextInt(possibleCharacters.length)];
}
String filename = "MyFile" + s1 + characters + ".txt";
File file = new File(filename);

if(file.exists() && !file.isDirectory()) { 
    // do something
}
mattsap
  • 3,790
  • 1
  • 15
  • 36
0

As far as I can tell, your problem is not how to create the file name, but rather, how to check if a file with that possible name exists.

If you know the formation rule for the names (suppose "aBeginning" + "aDatePresentation" + "anEnd"), then you can test for possible files, such as

boolean checkFileToday(){
   Date today = new Date();
   String name = "aBeginning"+today.getDate()+"anEnd";
   File file = new File(name);
   return file.exists();
}
Bonatti
  • 2,778
  • 5
  • 23
  • 42
0

The following code, generates new file with the unique name.

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class DynamicFile {

    public static void main(String[] args) throws IOException {
        int i = 4;
        do {
            //UUID creates random string.
            String randomID = UUID.randomUUID().toString();
            File file = new File("A:/NewFolder/MyFile-" + randomID.substring(0, 5) + ".txt");
            file.createNewFile();
        } while (i-- > 0);
    }
}

Will create files like:

MyFile-1d2ef.txt