0

I want to find if a file exists or not in java The name of the file can abc_*_*.xml (where * represents any character or number)

temp = File.createTempFile(startfilename + "_" + "" + ".xml");
boolean exists = temp.exists(); 

How to check for various combinations of filename where only the start of the file name is similar and the rest of the filename after _ is different

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
DarkNight
  • 21
  • 3

1 Answers1

0

Use the Files api

if(!Files.exists(Paths.get("yourpathhere"))){
    /*you code here*/    
}

and then you can use this in any cycle you need

as if you want to iterate items in directory, use Files.newDirectoryStream.

filter example here(stackoverflow)

Kampaii
  • 123
  • 2
  • 11
  • yes, but that way, you would need to have the actual path. The question is about when you don't have the actual path – Stultuske Dec 05 '19 at 10:00
  • Exactly the problem is i will have the path but the name of the file will be generated through the code only a part of the file name will match with the existing file name so i want to know how to match with only a part of the filename – DarkNight Dec 05 '19 at 11:29
  • then you shold use newDirectoryStream(path,filter) from [Files api](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) – Kampaii Dec 05 '19 at 11:55