gpt4 book ai didi

java - 重命名文件(如果存在)

转载 作者:行者123 更新时间:2023-12-02 07:32:31 26 4
gpt4 key购买 nike

当我尝试使用已存在的文件名保存文件时,我需要提示用户重命名该文件才能保存。如果目录中存在相同的文件,提示用户重命名文件的更好方法是什么?我需要通过用于保存文件的同一对话框来实现此操作。

File exportFile = new File(FilePath + "\\"+ FileName);
boolean exists = exportFile.exists();

if (!exists) {

System.out.println("File does not exists");
// TODO code here
}
else{

System.out.println( "File exists.");
// TODO code here
}

代码将在//TODO部分实现。我怎样才能做到这一点?

最佳答案

while(exists) {\\提示用户输入另一个名称并相应更改“存在”状态}

            while(exists) 
{
String temp=dis.readLine();
exists = (FilePath + "\\"+ temp).exists();
if(!exists) {exportFile = new File(FilePath + "\\"+ temp); break;}

}

其中 dis 是 DataInputStream 对象。祝你好运。

关于java - 重命名文件(如果存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12720311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com