How to rename a java file

Asked

Viewed 2,837 times

4

Given my code below:

import java.io.File;

public class App {

    public static void main(String[] args) {
        File diretorio = new File("/home/douglas/roms");
        File[] arquivos = diretorio.listFiles();

        for (File arquivo : arquivos) {
            String nome;
            String extencao = ".gba";
            nome = arquivo.getName();
            nome = nome.toLowerCase();
            nome = nome.substring(0, nome.indexOf(extencao));
            nome = cortarTrecho(nome, " # gba");
            String primeira = nome.substring(0, 1).toUpperCase();
            String restante = nome.substring(1);
            nome = primeira + restante;

            while (nome.contains(" ")) {

                String nomeFinal = "";
                String partes[] = nome.split("\\s+");

                for (int i = 0; i < partes.length; i++) {
                    nomeFinal += "*" + partes[i].substring(0, 1).toUpperCase() + partes[i].substring(1).toLowerCase();
                }
                nomeFinal = nomeFinal.substring(1) + extencao;
                nome = nomeFinal;

            }
            nome = nome.replace("*", " ");

        }

    }

    public static String cortarTrecho(String nome, String trecho) {

        int index = nome.indexOf(trecho);
        nome = nome.substring(0, index);
        return nome;

    }

}

Note that the file name is already as I wish, as I do for the file to be renamed with the new name ?

  • see if you resolve: http://stackoverflow.com/a/27534/5524514

3 answers

8


Try adding the following to your code:

arquivo.renameTo(new File(name + extencao));
  • @Douglas worked?

  • Yes, thank you. show

0

To remove numbers and other characters from the song file name:

public static void main(String[] args) {
        File dir = new File("C:\\Musicas");
        if (dir.exists()) {
            File[] files = dir.listFiles();
            if (files.length > 0) {
                for (File file : files) {
                    String f = file.getName();
                    String s = f.replaceAll("0|1|2|3|4|5|6|7|8|9|\\)|-|\\(", "").trim() + "3";
                    File oldName = new File("C:/program.txt");
                    File newName = new File("C:\\Musicas\\" + s);
                    if (file.renameTo(newName)) {
                        System.out.println("OK");
                    } else {
                        System.out.println("Erro");
                    }
                    System.out.println(s);
                }
            } else {
                System.out.println("dir vazio");
            }
        } else {
            System.out.println("dir não existe");
        }
    }
}

0

Another way to rename the file is by using the library Commons IO apache.

Example:

    FileUtils.moveFile(FileA, FileB);

To read the file, you can use the getFile of the library itself:

  FileUtils.getFile("src/test/resources/arquivo.txt"), 
  FileUtils.getFile("src/main/resources/"), true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.