大家好,我目前正在用Java编写一个方法,我正在尝试创建新文件,但我需要这些文件不是同名,而是增加名称值,如下所示:
/Users/Myself/Desktop/myFile0.xml
/Users/Myself/Desktop/myFile1.xml
/Users/Myself/Desktop/myFile2.xml
/Users/Myself/Desktop/myFile3.xml
所以我尝试在我的代码中执行以下操作,但是我不明白为什么当我在每个循环中调用该文件时(为了创建一个新的),数字不会增加?
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());
}
非常感谢任何帮助,谢谢!
答案 0 :(得分:1)
我在您的代码中看到的是,在递增数字之前,您将数字设置为ZERO。如果有的话,试着把int numbers=0
放出你的循环! (你没有在代码中写过任何循环)。当然,提供更多信息会有所帮助。
答案 1 :(得分:0)
你的for循环没有可以增加的计数器,因为它是for-each-loop(如果这是你所说的循环)。您也只需拨打chooserFolder.setSelectedFile(new File("myFile" + numbers++ + ".xml"));
一次,numbers++
就会出现chooserFolder.setSelectedFile(new File(chooserFolder.getSelectedFile().getAbsolutePath()));
。要给出正确的解决方案,您需要提供所有代码。这一行根本没有任何意义dialogRef = this.dialog.open(component, config);
。一旦你提供了所有代码,我们就可以提供解决方案
答案 2 :(得分:0)
请针对此问题使用时间戳解决方案
String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
下面有一个更好的例子
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");
}
}