Reputation: 73
I am currently writing a method in Java where I am trying to create new files but I need those files not to be of the same name, but rather of incrementing name values, like so:
/Users/Myself/Desktop/myFile0.xml
/Users/Myself/Desktop/myFile1.xml
/Users/Myself/Desktop/myFile2.xml
/Users/Myself/Desktop/myFile3.xml
So I have tried to do the following in my code, but I do not understand why when I call the file within the for each loop ( to create a new one) the number does not increment?
public void pickFolder() throws Exception {
chooserFolder.setDialogTitle("Specify your save location");
chooserFolder.setDialogType(JFileChooser.SAVE_DIALOG);
int numbers = 0;
chooserFolder.setSelectedFile(new File("myFile" + numbers++ + ".xml"));
chooserFolder.setFileFilter(new FileNameExtensionFilter("xml file", "xml"));
int userSelection = chooserFolder.showSaveDialog(null);
if (userSelection == JFileChooser.APPROVE_OPTION) {
for (File file : files) {
chooserFolder.setSelectedFile(new File(chooserFolder.getSelectedFile().getAbsolutePath()));
fileToSave = chooserFolder.getSelectedFile();
if (fileToSave.createNewFile()) {
System.out.println("File is created!");
fileToSave = chooserFolder.getSelectedFile();
} else {
JOptionPane.showMessageDialog(null, "File already exists.");
}
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}
Upvotes: 2
Views: 2067
Reputation: 1
I don't know how to check IF a file exists, but with a WHILE you must be able to do so:
while (f.isFile("myFile" + numbers + ".xml") == TRUE) {number++} // end while
chooserFolder.setSelectedFile(new File("myFile" + numbers + ".xml"));
Reference: How do I check if a file exists in Java?
Upvotes: 0
Reputation: 1214
Please use the timestamp solution for this problem
String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
Here have a better example below
package com.seleniummaster.examplefile;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CreateFileWithTimeStamp {
public static void main(String[] args)
{
CreateFileWithTimeStamp("test");
}
//Create a new file
public static void CreateFileWithTimeStamp(String filename) {
//get current project path
String filePath = System.getProperty("user.dir");
//create a new file with Time Stamp
File file = new File(filePath + "\\" + filename+GetCurrentTimeStamp().replace(":","_").replace(".","_")+".txt");
try {
if (!file.exists()) {
file.createNewFile();
System.out.println("File is created; file name is " + file.getName());
} else {
System.out.println("File already exist");
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Get current system time
public static String GetCurrentTimeStamp() {
SimpleDateFormat sdfDate = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");// dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;
}
// Get Current Host Name
public static String GetCurrentTestHostName() throws UnknownHostException {
InetAddress localMachine = InetAddress.getLocalHost();
String hostName = localMachine.getHostName();
return hostName;
}
// Get Current User Name
public static String GetCurrentTestUserName() {
return System.getProperty("user.name");
}
}
Upvotes: 0
Reputation: 6435
your for-loop has no counter which can be increased, because it is a for-each-loop (if that is the loop you mean). also you call chooserFolder.setSelectedFile(new File("myFile" + numbers++ + ".xml"));
only once and there is the only occurrence of numbers++
. To given an proper solution you would need to provide all the code. also this line makes no sense at all chooserFolder.setSelectedFile(new File(chooserFolder.getSelectedFile().getAbsolutePath()));
. once you give all the code we can provide a solution
Upvotes: 0
Reputation: 516
What I'm seeing in your code is that you set numbers to ZERO right before incrementing it. try putting int numbers=0
out of your loop if there is any! (you have not written any loop in the code). And of course giving more information would be helpful.
Upvotes: 2