附加值使文件从文件夹中消失

发布于 2025-01-20 00:47:45 字数 1126 浏览 0 评论 0原文

import java.io.File;
import java.util.*;


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();
    
    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]);
            
            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[i].renameTo(dest);
            System.out.println(files[i]);
            }   
    }
    }
  }

该工具旨在将值附加到目录中每个文件名的文件名的开始。它似乎是在附加值的附加值,但是它从目录中删除文件,而不是在同一目录中重命名现有文件。任何帮助将不胜感激。

import java.io.File;
import java.util.*;


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();
    
    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]);
            
            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[i].renameTo(dest);
            System.out.println(files[i]);
            }   
    }
    }
  }

This tool is designed to append a value to the beginning of the filename for every filename in a directory. It seems to append the value as it should, but it deletes the files from the directory rather than rename the existing file within the same directory. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

七秒鱼° 2025-01-27 00:47:45

要重命名同一目录中的文件,可以使用:
Files.move(source, source.resolveSibling("newName.txt"),
StandardCopyOption.REPLACE_EXISTING);

你的结果变量很奇怪,你可以:
字符串结果 = valInput + files[i].getName();

To rename a file in the same directory, you can use:
Files.move(source, source.resolveSibling("newName.txt"),
StandardCopyOption.REPLACE_EXISTING);

Your result variable is weird, you can just:
String result = valInput + files[i].getName();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文