使用Java创建具有相似名称的文件,而不覆盖现有文件 [英] Create files with similar names using Java without overwriting existing file

查看:138
本文介绍了使用Java创建具有相似名称的文件,而不覆盖现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以创建多个具有相似名称的文件,而不会覆盖当前文件。

I was wondering if it is possible to create multiple files with similar names, without overwriting the current file.

例如:
如果我有一个file:xyz.txt
下次我创建时应该是:xyz(1).txt

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();
}

所以如果我重新运行这个程序,我的当前文件不应该被覆盖。我已经尝试过if if with a flag变量将number添加为文件名的前缀。

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.

我想知道是否有任何Java命令可以避免覆盖现有文件。

I want to know if there are any Java commands to avoid overwriting an existing file.

推荐答案


我想知道是否有任何本机java命令要停止覆盖[并附加一个数字到文件名]

i want to know if there are any native java commands to stop overwriting [and append a numeral to the filename]

不在核心Java库中,没有。

Not in the core Java libraries, no.

这篇关于使用Java创建具有相似名称的文件,而不覆盖现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆