Ho fatto già da subito quello che mi hai detto
... e la lista dei file contenuti in quel path la vedo tutta con tutti i nomi corretti.
Ho risolto riscriverdo tutti i file in una nuova cartella con il nome che mi serviva, però questa operazioni è del tutto poco effiente... ho scritto il codice qui in basso.
Sai dirmi un modo per rinominare semplicemente i file?
codice:
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println("");
String files[] = getLlistFile(PATH_ANIME_FOLDER);
printStringArray(files);
File[] f = getListFile(PATH_ANIME_FOLDER);
saveFile(f, PATH_FOLDER);
}
/**
* Prende tutti i nomi dei file contenuti nella cartella.
* @param path - path della cartella.
* @return files - array di stringa inside the name of all file.
*/
public static String[] getLlistFile(String path) {
File f = new File(path);
String[] files = f.list();
return files;
}//end method
/**
* Prende tutti i file nella cartella specificata.
*
* @param path - path della cartella
* @return files - array di file
*/
public static File[] getListFile (String path) {
File f = new File(path);
File[] files = f.listFiles();
return files;
}
/**
* Stampa tutti i valori all'interno di un array di stringa
* @param string - array of string to print every element.
*/
public static void printStringArray(String[] string){
for (int i = 0; i < string.length; i++)
System.out.println(string[i]);
}//end method.
/**
*
* @param t - lista dei file
* @param sv - percorso dove mettere i nuovi file
* @throws IOException
*/
private static void saveFile(File[] t, String sv) throws IOException
{
for (int i = 0; i < t.length; i++) {
File nFile = new File(sv+"/" + getTextTo000(t.length, i));
System.out.println(nFile.getName());
System.out.println(nFile.getPath());
riscrizioneFile(t, i, nFile);
}
}//end method
/**
*
* @param count
* @param i
* @return
*/
private static String getTextTo000 (int count, int i){
String name = "";
if (i <= 9)
name = "00" + i;
else if (i > 9)
name = "0" + i;
else if (i > 99)
name = "" + i;
else
System.err.print("Errore calcolo del testo " + " *** " + i);
return name + ".jpg";
}
/**
* Scrive fisicamente i file su disco
*
* @param t - array di file.
* @param i - indice.
* @param nFile - File da scrivere con il nome corretto.
* @throws IOException
*/
private static void riscrizioneFile (File[] t, int i, File nFile) throws IOException{
try {
FileChannel sourceChannel = new FileInputStream(t[i]).getChannel();
FileChannel destinationChannel = new FileOutputStream(nFile).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
sourceChannel.close();
destinationChannel.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}