在没有错误的情况下将文件重命名为目录的问题:“该过程无法访问该文件,因为另一个过程正在使用该文件。”
import java.io.File;
import java.io.IOException;
import java.util.*;
//import java.lang.Object;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class AppendTool {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Input Directory to Append Values: ");
String dirInput = sc.nextLine();
System.out.println("Input value to append to Directory Files: ");
String valInput = sc.nextLine();
sc.close();
Path source = Paths.get(dirInput);
File path = new File(dirInput);
File [] files = path.listFiles();
for (int i = 0; i < files.length; i++){
if (files[i].isFile()){ //this line weeds out other directories/folders
System.out.println(files[i]);
try {
// int where = files[i].getName().lastIndexOf(".");
String result = valInput + files[i].getName();//.substring(0, where) + files[i].getName().substring(where);
System.out.println(result);
File dest = new File(result);
//Files.move(files[i].toPath(), dest.toPath().resolveSibling(dest.getName()), StandardCopyOption.REPLACE_EXISTING);
Files.move(source, source.resolveSibling(dest.getName()), StandardCopyOption.REPLACE_EXISTING);
System.out.println(dest);
return;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我正在尝试遍历目录中的所有文件,并将每个文件名附加一个值。使用files.move创建FilesystemException,并指出文件正在另一个过程使用。使用注释输出文件.move(文件[i] .topath .....)删除创建新文件,从目录中删除旧文件,但不会替换原始文件。任何帮助将不胜感激。对于下面的错误,我有一个保存在文档中的tump.txt文件,我想将“ e”附加到文件名中。
c:\ users \ bob \ documents-&gt; C:\ users \ bob \ estuff.txt:该过程无法访问该文件,因为它是由另一个进程
使用的
在java.base/sun.nio.fs.windowsexception.translatetoioexception(windowsexception.java:92) 在java.base/sun.nio.fs.windowsexception.rethrowasioexception(windowsexception.java:103) 在java.base/sun.nio.fs.windowsfilecopy.move(windowsfilecopy.java:395) at Java.base/sun.nio.fs.windowsfilesystemprovider.move(windowsfilesystemprovider.java:292) 在java.base/java.nio.file.files.move(files.java:1426) 在appendtool.main(appendtool.java:36)
上
import java.io.File;
import java.io.IOException;
import java.util.*;
//import java.lang.Object;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class AppendTool {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Input Directory to Append Values: ");
String dirInput = sc.nextLine();
System.out.println("Input value to append to Directory Files: ");
String valInput = sc.nextLine();
sc.close();
Path source = Paths.get(dirInput);
File path = new File(dirInput);
File [] files = path.listFiles();
for (int i = 0; i < files.length; i++){
if (files[i].isFile()){ //this line weeds out other directories/folders
System.out.println(files[i]);
try {
// int where = files[i].getName().lastIndexOf(".");
String result = valInput + files[i].getName();//.substring(0, where) + files[i].getName().substring(where);
System.out.println(result);
File dest = new File(result);
//Files.move(files[i].toPath(), dest.toPath().resolveSibling(dest.getName()), StandardCopyOption.REPLACE_EXISTING);
Files.move(source, source.resolveSibling(dest.getName()), StandardCopyOption.REPLACE_EXISTING);
System.out.println(dest);
return;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
I'm trying to iterate through all files in a directory and append a value to each filename. Using Files.move creates a FileSystemException and states that the file is being used by another process. Using the commented-out Files.move(files[i].toPath.....) deletes creates the new file, deletes the old one from the directory, but it does not replace the original file. Any help would be appreciated. For the error below, I have a stuff.txt file saved in documents and I wanted to append "e" to the filename.
C:\Users\Bob\Documents -> C:\Users\Bob\estuff.txt: The process cannot access the file because it is being used by another process
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:395)
at java.base/sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:292)
at java.base/java.nio.file.Files.move(Files.java:1426)
at AppendTool.main(AppendTool.java:36)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您会遇到错误,因为 files.move()方法是错误的。您需要提供源文件的完整路径和文件名 (原始文件名)和
source.resolvesibling()
方法需要要提供的目标文件的完整路径和文件名 在这种情况下,该文件将发生在原始文件名中,并将所需的文本插入到其开头。这是如何实现这一目标的快速示例:
You are getting the error because the the parameter arguments for the Files.move() method are wrong. You need to supply the full path and file name of the source file (the original file name) and the
source.resolveSibling()
method need to be supplied the full path and file name of the destination file which in this case happens to the the original file name with the desired text inserted into the beginning of it.Here is a quick example of how to achieve this: