public String getUniqueFilename ( String baseFileName , String extension , boolean cleanBaseFileName , String ... ancestorFolders ) { if ( cleanBaseFileName ) { baseFileName = PathUtil . cleanFileName ( baseFileName ) ; } if ( ancestorFolders != null ) { Path pathToFile = Paths . get ( "" , Stream . of ( ancestorFolders ) . map ( ancestor -> PathUtil . cleanFileName ( ancestor ) ) . toArray ( String [ ] :: new ) ) . resolve ( baseFileName ) ; baseFileName = pathToFile . toString ( ) ; } String filename = baseFileName + "." + extension ; // FIXME this looks nasty while ( usedFilenames . contains ( filename ) ) { filename = baseFileName + "." + index . getAndIncrement ( ) + "." + extension ; } usedFilenames . add ( filename ) ; return filename ; }
Returns a unique file name