Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Java中有没有办法创建一个文件并在该文件已经存在时将其重命名,例如“Dog.jpg”,类似于“Dog(1).jpg”等等(“Dog(2).jpg”如果“Dog(1).jpg”也存在,等等)?
Filejava中的类有一个叫做existsuse的方法来检查文件是否存在
File
exists
如果确实存在,请将其重命名为其他名称。
重复此过程,直到找到不存在的文件名
是的,有一种方法可以创建一个文件,但如果它已经存在,则将其命名为不同的名称。你看过这个类的javadocsFile吗?具体来说,exists()方法。如果返回 true,请尝试创建“Dog(1).jpg”,如果exists()再次调用。等等
exists()