Reputation: 47
So I have a file path which includes the filename along with extension and have appended System.currentmillis().Now If a condition is met I would like to remove the System.CurrentMillis from it .How can I do that?.The file path looks something like this
String filePath = someClass.getFile()+System.currentTimeMillis()+".txt";
Any help would be appreciated.Thanks in advance
Upvotes: 0
Views: 73
Reputation: 325
The length of System.currentTimeMillis() now is 13 and it will take more than 200 years for its size to increase by 1. Hence I am trying to find the index of extension and using it to remove the timestamp from the file path.
String fileName = "fileName1576647662124.txt";
String extension = ".txt";
int extensionIndex = fileName.indexOf(extension);
System.out.println(fileName.substring(0, extensionIndex - 13) + extension);
Upvotes: 0
Reputation: 522516
We can't answer this in general without knowing what the file name is, in particular with which character that file name might end. If we assume that the file name would not end in a digit, we can try:
String filePath = "some_file1576645285164.txt";
System.out.println(filePath);
String output = filePath.replaceAll("\\d+(\\.[^.]+)$", "$1");
System.out.println(output);
This prints:
some_file1576645285164.txt
some_file.txt
Upvotes: 1