Reputation: 1820
I was wondering if it is possible to create multiple files with similar names, without overwriting the current file.
for example: if I have a file: xyz.txt next time when i create it should be : xyz(1).txt
try {
File makefile = new File("output.txt");
FileWriter fwrite = new FileWriter(makefile);
fwrite.write("example file");
fwrite.flush();
fwrite.close();
} catch (IOException e) {
e.printStackTrace();
}
so if I re-run this program my current file should not be overwritten. I have already tried and if condition with a flag variable to add number as prefix to the file name.
I want to know if there are any Java commands to avoid overwriting an existing file.
Upvotes: 6
Views: 7418
Reputation: 497
static String getUniquePath(String path,String extension){
int i = 1;
String newPath = path + extension;
if(new File(newPath).exists()) {
while (new File(newPath).exists()){
newPath = path + "_" + i + extension;
i++;
}
}
return newPath;
}
Upvotes: 0
Reputation: 4448
For kotlin
Here is the code for create files with similar names using Kotlin without overwriting existing file:
var fileNo = 0
@Throws(IOException::class)
fun createFile(name: String, addedEx: String): File {
val originalName = name.replace(addedEx,"")
val f = File(name)
return if (!f.exists()) {
f
} else {
fileNo++
val file = createFile("${name.substring( 0, originalName.indexOf("."))}_($fileNo).${getExtension(originalName)}","_($fileNo)")
fileNo = 0
file
}
}
Remember when you call it for the first time just pass empty string in addedEx
param. Like this : createFile(nameTest.txt, addedEx: "")
if save this file 5 times then, Output will be:
nameTest.txt
nameTest_(1).txt
nameTest_(2).txt
nameTest_(3).txt
nameTest_(4).txt
if you want's the extension getting function:
fun getExtension(name: String?): String? {
val fileNameFromURL = Uri.parse(name).lastPathSegment
if(fileNameFromURL != null && fileNameFromURL.lastIndexOf('.') >= 0)
{
return fileNameFromURL.substring(fileNameFromURL.lastIndexOf('.')+1)
}
return ""
}
Upvotes: 0
Reputation: 133
While similar to Bitmap's response, I came up with this java code (which seems to work). I don't know if it's the most efficient, but...
For my example, I am assuming the file will be a .txt file
String fileName = "output.txt";
File aFile = new File(fileName);
int fileNo = 0;
while(aFile.exists() && !aFile.isDirectory()) {
fileNo++;
String newName = fileName.replaceAll(".txt", "(" + fileNo + ").txt");
aFile = new File(newName);
}
Upvotes: 2
Reputation: 11767
Take a look at the method File.createTempFile()
To create the new file, the prefix and the suffix may first be adjusted to fit the limitations of the underlying platform. If the prefix is too long then it will be truncated, but its first three characters will always be preserved. If the suffix is too long then it too will be truncated, but if it begins with a period character ('.') then the period and the first three characters following it will always be preserved. Once these adjustments have been made the name of the new file will be generated by concatenating the prefix, five or more internally-generated characters, and the suffix.
Upvotes: 2
Reputation: 12538
Not in java as already indicated by @Greg Kopff
.
But you can work around something like this:
int count = 0;
public void createFile(String name) throws IOException
{
File f;
f = new File(name);
if (!f.exists())
{
f.createNewFile();
}
else
{
count++;
createFile(name + (count));
}
}
Upvotes: 4
Reputation: 16545
i want to know if there are any native java commands to stop overwriting [and append a numeral to the filename]
Not in the core Java libraries, no.
Upvotes: 2