在没有错误的情况下将文件重命名为目录的问题:“该过程无法访问该文件,因为另一个过程正在使用该文件。”

发布于 2025-01-19 17:09:16 字数 2349 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2025-01-26 17:09:16

您会遇到错误,因为 files.move()方法是错误的。您需要提供源文件的完整路径和文件名 (原始文件名)和source.resolvesibling()方法需要要提供的目标文件的完整路径和文件名 在这种情况下,该文件将发生在原始文件名中,并将所需的文本插入到其开头。

这是如何实现这一目标的快速示例:

Scanner sc = new Scanner(System.in);
System.out.println("Input Directory path to modify file names in:");
System.out.print("Directory Path: --> ");
String dirInput = sc.nextLine();

/* 'DO' take the time to ensure that OS file naming rules 
   are strictly followed when accepting User Input!!   */
System.out.print("Enter the text you want to insert into\n"
               + "the begining of each file name within\n"
               + "this directory: --> ");
String valInput = sc.nextLine();

// Close ONLY if you won't ever need it again in this application.
sc.close(); 
System.out.println();
    
File path = new File(dirInput);
File[] files = path.listFiles();

for (File file : files) {
    if (!file.isFile()) { continue; }
    Path source = Paths.get(file.getAbsolutePath());
    File newName = new File(file.getParent() + File.separator + valInput + file.getName());
    System.out.println(newName.getAbsolutePath());
    try {
        Files.move(source, source.resolveSibling(newName.getAbsolutePath()),
                                       StandardCopyOption.REPLACE_EXISTING);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

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:

Scanner sc = new Scanner(System.in);
System.out.println("Input Directory path to modify file names in:");
System.out.print("Directory Path: --> ");
String dirInput = sc.nextLine();

/* 'DO' take the time to ensure that OS file naming rules 
   are strictly followed when accepting User Input!!   */
System.out.print("Enter the text you want to insert into\n"
               + "the begining of each file name within\n"
               + "this directory: --> ");
String valInput = sc.nextLine();

// Close ONLY if you won't ever need it again in this application.
sc.close(); 
System.out.println();
    
File path = new File(dirInput);
File[] files = path.listFiles();

for (File file : files) {
    if (!file.isFile()) { continue; }
    Path source = Paths.get(file.getAbsolutePath());
    File newName = new File(file.getParent() + File.separator + valInput + file.getName());
    System.out.println(newName.getAbsolutePath());
    try {
        Files.move(source, source.resolveSibling(newName.getAbsolutePath()),
                                       StandardCopyOption.REPLACE_EXISTING);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文