Renomeação de arquivos e operações em movimento
Às vezes, para acessar e gerenciar arquivos de maneira unificada, é necessário renomear os arquivos e movê -los para uma nova pasta. Como implementá -los?
Um applet java simples pode ser implementado:
part_1:Requirements: I need to rename the video files under all subfolders in the <(E:/BaiduYun/Chuanzhi Podcast_Zhang Xiaoxiang_Java Multithreading and Concurrency Library Advanced Application Video Tutorial Download)> folder and move them to a new location <(E:/BaiduYun/Zhang Xiaoxiang_Java Multithreading and Concurrency Library)>;
parte_2: a estrutura do diretório é a seguinte:
E:/Baiduyun
E:/Baiduyun/Chuanzhi Podcast_zhang xiaoxiang_download Application Video Application Video Tutorial para Java Multithreading and Concurrency Library
E:/Baiduyun/Chuanzhi Podcast Zhang Xiaoxiang_java Multi-threading and Concurrency Library Application Application Video Tutorial Download/01Chuanzhi Podcast Zhang Xiaoxiang's Tradicional Technology Review
parte_3: código -fonte do programa + comentários:
pacote cn.mike.demo; importar java.io.file; importar java.io.fileNotFoundException; importar java.util.arrayList; importar java.util.list;/** * @Author Administrator * @Usage Este programa implementa renomeação e operações de movimentos; */public class Renamefiles {arquivo estático privado srcfolder; // pasta de origem Private Static Arquive DestFolder; // Pasta de destino Lista estática privada <File> srcfiles; // armazenar todos os arquivos a serem nomeados static {srcfolder = new File (e: // baidiayun // chuanzhi PoDAr. APLICATIVO DOWNLOAD TUTORIAL DO TUTORIAL DO VÍDEO "); DestFolder = new File (" E: // Baiduyun // Zhang Xiaoxiang_java MultithReading and Concurrency Library "); srcfiles = new ArrayList <File> (); (FileNotFoundException e) {e.printStackTrace();return;}// traverse the source folder and put the file to be modified into the collection iterateGetFiles(RenameFiles.srcFolder);// Rename the elements in the collection (and move to the target folder) iterateRename();}// end method-mainprivate static void checkFolder() throws FileNotfoundException {if (! Renamefiles.srcfolder.exists ()) {lança novo fileNotFoundException ("a pasta de origem especificada não existe.");} If (! RenameFiles.DestFolder.exists ()) {throw filenOnToDoDeDexception ("o alvo especificado não. {String aviname = null; string tempstr = null; stringbuilder strbuilder = new stringbuilder (); arquivo tempfile = null; string sequencenumber = null; string detalhelname = null; // traver a coleta da lista e renogue um por um para um (arquivo cada um: renamefiles.sr (. cada.getName (). Length () - 4); // Obtenha o nome do arquivo (excluindo o nome do sufixo ".avi") tempstr = cada.getParent (); // o nome da pasta pai seqüencenumber = string.Format ("%02d", inteiro.Valueof (AViname); 01DetailName = tempstr.substring (tempstr.lastindexof ("_") + 1); // o conteúdo detalhado do arquivo de vídeo, por exemplo: Tecnologia de exclusão mútua tradicional de thread strbuilder.append (sequencenumber + "_" + detalhename + "Aavi"); tempfile = strbuilder.toString ()); // caminho do novo arquivo // cada.renameto (tempfile); // Código Core (renomear e mover) System.out.println (tempfile.tostring (); // imprimir no console para a depuração strbuilder.delete (0, strbuilder.lnkngths (end); foreach} // End Metod-iteraterenameprivate estático void iterateGetFiles (arquivo srcfile) {// Se for uma pasta, continue a percorrer em profundidade se (srcfile.isdirectory ()) {files [] files = srcfile.listfiles (); (srcfile.getabsolutepath (). endswith (".vi")) {// Se não for uma pasta e o formato do arquivo é Avi, adicione o arquivo à coleção da lista dos arquivos a serem nomeados renamefiles.srcfiles.add (srcfile);}}} // end métod-iterategetgetgetgetgetgetgetgetgetgetgetgetgetgetgetget.Adfiles.parte_4: o efeito após renomear e mover:
E:/Baiduyun/Zhang Xiaoxiang_java Multithreading and Concurrency Library
Resumir
O exposto acima é o conteúdo inteiro deste artigo sobre o código da instância de renomeação e operação em movimento dos arquivos Java. Espero que seja útil para todos. Amigos interessados podem continuar se referindo a outros tópicos relacionados neste site. Se houver alguma falha, deixe uma mensagem para apontá -la. Obrigado amigos pelo seu apoio para este site!