20177 月30
同名文件自动建立副本递归实现
public void newFile(File file){ try { if (file.createNewFile()) { System.out.println("文件创建成功!"); } else { String cName = changeName(file); File files = new File("." + File.separator + cName); newFile(files); } } catch (IOException e) { e.printStackTrace(); } }
public String changeName(File file) { String name = file.getName(); int index = name.lastIndexOf('.'); int index2 = name.lastIndexOf("_副本"); if (index2 < 0) { return name.substring(0, index) + "_副本1" + name.substring(index); } else { String num = name.substring(index2+3, index); int i = Integer.valueOf(num)+1; return name.substring(0, index2) + "_副本" + i + name.substring(index); } }
本文地址:https://wizzer.cn/archives/3355 , 转载请保留.