簡體   English   中英

如何使用Java重命名Linux目錄中的所有文件?

[英]How to rename all files in a directory in linux using java?

我正在嘗試實現POP3協議功能,並且我想使用文件系統(其中的目錄和文本文件)作為數據庫來存儲電子郵件。 為此,我每次訪問數據庫時都需要對.txt文件(email1.txt,email2.txt等)進行重新編號,以檢查天氣情況,其中任何電子郵件都被刪除了。 假設email2.txt已被刪除,這意味着在下一個事務中,所有電子郵件將被重新編號,email3.txt將重命名為email2.txt,email4變為email3等等。 如果沒有一個被刪除,則所有文件應保持不變

我嘗試使用以下代碼,但無法正常工作。 但是,它可以在Windows上正常工作。 我知道,重命名文件取決於操作系統。

    File dir = new File(absolutePath);
    File[] filesInDir = dir.listFiles();
    int i = 0;
        for(File file1:filesInDir) {
        i++;
        String oldName = file1.getName();
        oldName = absolutePath + "/" + oldName;
        File oldFile=new File(oldName);
        String newName = "email" + i + ".txt";
        newName = absolutePath + "/" + newName;
        File newFile =new File(newName);
        oldFile.renameTo(newFile);
    }

您好,我對您的代碼進行了更改:

    public static void main(String[] args) {

    String absolutePath = "/Users/jucepho/Desktop/ReaderPaths/src/other/";

    File dir = new File(absolutePath);
    File[] filesInDir = dir.listFiles();
    int i = 0;
        for(File file1:filesInDir) {
        i++;
        String oldName = file1.getName();
        oldName = absolutePath + File.separator+ oldName;
        File oldFile=new File(oldName);
        String newName = "email" + i + ".txt";
        newName = absolutePath + File.separator+ newName;
        File newFile =new File(newName);
        oldFile.renameTo(newFile);
    }

}

它將目錄中的所有文件重命名為email1.txt email2.txt等...

它應該可以工作,我在Ubuntu上測試過它:)

好吧,我想您可以查找所有文件,看看它們是否存在,然后執行任何您想做的事情:O當然,我還沒有完成它,但是也許可以幫助您=)

  public static void main(String[] args) {

        String absolutePath = "/Users/jucepho/Desktop/src/other/";

        File dir = new File(absolutePath);
        File[] filesInDir = dir.listFiles();
        List<File> filesDirectory = Arrays.asList(filesInDir);
        List<Integer> numbersUsed = new ArrayList<Integer>();

        for(File files2: filesDirectory ){
            String nameFile = files2.getName();
            System.out.println(nameFile);
             String regex = "email.\\.txt";
             boolean dosItMatch = nameFile.matches(regex);
             if(dosItMatch){

                 String number = "\\d+";

                 numbersUsed.add(Integer.valueOf(regex.replace("email", "").replace("\\.txt", "")));
             }
               System.out.println(dosItMatch);
        }
        System.out.println(numbersUsed);
        int i = 0;
            for(File file1:filesInDir) {
            i++;
            String oldName = file1.getName();
            oldName = absolutePath + File.separator+ oldName;
            File oldFile=new File(oldName);
            String newName = "email" + i + ".txt";
            newName = absolutePath + File.separator+ newName;
            File newFile =new File(newName);
            oldFile.renameTo(newFile);
        }

    }

最后,我僅通過首先對(filesInDir)數組進行排序就擺脫了它。

        File dir = new File(absolutePath);
        File[] filesInDir = dir.listFiles();
        Arrays.sort(filesInDir);
        int i = 0;
            for(File file1:filesInDir) {
            i++;
            String oldName = file1.getName();
            s.getBasicRemote().sendText("+OK "+oldName);
            oldName = absolutePath + "/"+ oldName;
            File oldFile=new File(oldName);
            String newName = "email" + i + ".txt";
            newName = absolutePath + "/"+ newName;
            s.getBasicRemote().sendText("+OK "+newName);
            File newFile =new File(newName);
            oldFile.renameTo(newFile);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM