简体   繁体   中英

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

I am trying to implement POP3 protocol functionalities and i want to use the file system (directories and text files in it) as a database to store emails. To do so, i need to renumber the .txt files (email1.txt, email2.txt,..) every time i access the database, to check weather any of the emails got deleted. Let's say email2.txt has been deleted, that means in the next transaction all emails will be renumbered and email3.txt will get renamed to email2.txt, email4 becomes email3 ans so on. and if none of them is deleted then all files should remain unchanged

I tried using following code, but it does not work. However, it works fine with windows. i know, renaming a file is OS dependant.

    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);
    }

Hello I made this change to your code:

    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);
    }

}

It renamed all my files in my directory to email1.txt email2.txt etc....

it should work i tested it on Ubuntu :)

Well I was thinking you could look for all files and see if they exist and do whatever you want look something like this :O ofcourse i haven't finished it but maybe it could help you =)

  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);
        }

    }

Finally , i got rid of it by just sorting (filesInDir) array first.

        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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM