Java中重命名平面文件的替代方法
因此,我正在编写一个程序,读取用户从特定文件夹提供名称的txt文件 我想在用户打开.txt后重命名它。然而,什么也没有发生Java中重命名平面文件的替代方法,java,file,rename,Java,File,Rename,因此,我正在编写一个程序,读取用户从特定文件夹提供名称的txt文件 我想在用户打开.txt后重命名它。然而,什么也没有发生 temp = userInput; currentFileDir = "D:\\Document\\" + username + "\\" + temp1 ; File directory = new File(currentFileDir + ".txt"); Sca
temp = userInput;
currentFileDir = "D:\\Document\\" + username + "\\" + temp1 ;
File directory = new File(currentFileDir + ".txt");
Scanner readingFile = new Scanner(directory);
while (readingFile.hasNextLine())
{
txtdata = txtdata + readingFile.nextLine() + " ";
}
File newName = new File(currentFileDir + "--OPENED.txt");
directory.renameTo(newName);
System.out.println(txtdata);
}
我提供的代码有问题吗
我已经尝试在一个独立的程序中使用它,它工作得很好,所以我认为程序的其余代码必须干扰重命名过程
是否有“重命名”的替代方法?从阅读开始。请记住,您将文件“移动”到的目录应该已经存在exist@MadProgrammer文件已存在,目录正确。我可以使用while循环从txt文件中读取数据,但是我不能重命名文件本身。在重命名之前关闭扫描仪。请不要调用包含文件名
目录的变量。是的,肯定有一个替代方法:。它优于java.io.File类,因为它总是会成功或引发信息异常。