每次执行代码时,我都希望创建一个新的文本文件。
文本文件应该被称为Person1。
下次执行代码时,文本文件应该被称为Person2。
然后再将文本文件称为Person3。等等....
目前,我能够创建名为" Person1"的文本文件。但无法创建另一个名为" Person2"。
的文本文件private int fileNumber = 1;
fileNumber = fileNumber++;
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("Person" + fileNumber + ".txt");
PrintWriter pw = new PrintWriter(fw);
pw.println("Hello you created a text file");
pw.close();
}
catch (IOException e)
{
System.out.println("Error!");
}
}
答案 0 :(得分:4)
创建新文件后,您应检查包含fileNumber
或index
的文件是否已存在。 虽然存在此类index
的文件,但index
应递增。最后,您创建一个不存在index
的新文件。
假设您创建了一个文件的抽象表示,现在想将其重命名为第一个可用的index
。我们假设其他文件位于C:\tmp
:
File newFile;
int index = 1;
String parent = "C:\\tmp"
String name = "Person";
while ((newFile = new File(parent, name + index)).exists()) {
index++;
}
/* Here you have a newFile with name set to the first available index */
或者如果你想考虑扩展名:
File newFile;
int index = 1;
String parent = "C:\\tmp"
String name = "Person";
String extension = ".txt";
while ((newFile = new File(parent, name + index + extension)).exists()) {
index++;
}
/* Here you have a newFile with name set to the first available index and extension */
UPDATE :使用Java 8 NIO API,我创建了以下方法,为文件中尚不存在的第一个可用路径返回Path
个对象系统:
/**
* Returns the first available {@code Path} with a unique file name. The
* first available path means that, if a file with the specified
* <tt>path</tt> exists on disk, an index is appended to it. If a file with
* that path still exists, index is incremented and so on, until a unique
* path is generated. This path is then returned.
* <p>
* If a file with the specified <tt>path</tt> does not exist, this method
* trivially returns <tt>path</tt>.
* <p>
* For an example, if the parent directory of the specified path already
* contains <tt>file.txt</tt>, <tt>file-0.txt</tt> and <tt>file-1.txt</tt>,
* and the file name of this path is <tt>file.txt</tt>, then a path with
* file name <tt>file-2.txt</tt> is returned.
*
* @param path path from which the first available is returned
* @return a path with a unique file name
*/
public static Path firstAvailable(Path path) {
if (!Files.exists(path))
return path;
int namingIndex = 0;
String name = path.getFileName().toString();
String extension = "";
int dotIndex = name.lastIndexOf('.');
if (dotIndex > 0) {
extension = name.substring(dotIndex);
name = name.substring(0, dotIndex);
}
name += "-";
while (Files.exists(path)) {
path = path.resolveSibling(name + namingIndex + extension);
namingIndex++;
}
return path;
}
答案 1 :(得分:4)
检查文件。如果存在则增加索引
File file = new File("E:\\" + "Person1" + ".txt");
int increase=1;
while(file.exists()){
increase++;
file = new File("E:\\" + "Person" + increase+ ".txt");
}
if(!file.exists()) {
try {
String content = textfile.toString();
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
}catch (IOException e){
}
答案 2 :(得分:0)
根据您的程序执行,有两种不同的方法。如果你想保持程序运行并在其中调用该函数,那么你可以保留最后生成的文件号并增加1.例如,你最后生成的文件名是Person4,所以,下一个将是Person5,通过递增变量的数量。但是如果你希望每次都从头开始运行程序,那么你必须阅读已写入你的目录的索引。当您从目录中读取文件名时,可以使用substring(5,index.length)来为您提供数字。然后为下一个文件增加该数字。
答案 3 :(得分:0)
试试这个。只需更改 for 循环中的 i
值即可创建您需要的文件数量
File file;
for (int i = 21; i < 32; i++) {
file = new File("Practica" + Integer.toString(i) + ".java");
try {
file.createNewFile();
} catch (Exception e) {}
}