Replace multiple file characters
1 1 import java. io. file; 2 2 import java. util. objects; 3 3 3 // replace multiple file name characters 4 4 4 public class Test07 {5 6 public static void reName () {6 7 while (true) {7 8 seconds SC = new seconds (System. in); 8 9 String filePath = ""; // input path 9 10 File file = new File (""); // File object 10 11 String dirPath = null; // absolute path 11 12 // isDirectory () test whether the file represented by this abstract path name is a directory 12 13 while (! File. isDirectory () {13 14 System. out. println ("Enter the folder you want to view (e: \ io)"); 14 15 filePath = SC. nextLine () + "\"; 15 16 file = new File (filePath); 16 17 // getAbsolutePath () returns the absolute path of this abstract path. 17 18 dirPath = file. getAbsolutePath (); 18 19} 19 20 System. out. println (dirPath); 20 21 // returns an array of abstract path names, which indicate the files in the directory represented by this abstract path name. 21 22 File [] fileList = file. listFiles (); 22 23 System. out. println ("the File in the path you entered is as follows:"); 23 24 for (File file2: fileList) {// use the foreach loop body 24 25 if (file2.isFile ()) {// if it is a file, get the file name and output 25 26 String formFile = file2.getName (); 26 27 System. out. println (formFile); 27 28} 28 29} 29 30 System. out. println ("Enter the old character you want to replace:"); 30 31 String oldStr = SC. nextLine (); 31 32 System. out. println ("Enter the new character you want to replace:"); 32 33 String new Str = SC. nextLine (); 33 34 int count = 0; // declare an int-type count, used to record the number of successfully replaced files 34 35 for (int I = 0; I <fileList. length; I ++) {35 36 String name = fileList [I]. getName (); // get the file name 36 37 name = name. replace (oldStr, newStr); // replace the file name containing oldStr characters 37 38 name = dirPath + "\" + name; // File name 38 39 File toFile = new File (name); // convert name to File object 39 40 if (fileList [I]. exists ()&&! ToFile. exists () {// the new file does not exist before it can be changed to 40 41 fileList [I]. renameTo (toFile); // rename (); File method. The user renamed 41 42 count ++; // number of successfully replaced files + 142 43} 43 44} 44 45 System. out. println ("replaced successfully, total number of files:" + fileList. length + ", succeeded" + count45 46 + ", failed" + (fileList. length-count) +. "); 46 47 System. out. println ("failed File:"); 47 48 for (File file2: fileList) {// output File name not replaced 48 49 if (file2.isFile ()) {49 50 String formFile = file2.getName (); 50 51 System. out. println ("\ t" + formFile); 51 52} 52 53} 53 54 SC. close (); 54 55 System. out. println ("close after 3 seconds"); 55 56 new Thread () {// create a Thread. After three seconds, output 56 57 @ Override57 58 public void run () {58 59 try {59 60 sleep (3000); 60 61} catch (InterruptedException e) {61 62 e. printStackTrace (); 62 63} 63 64} 64 65 }. start (); 65 66 break; 66 67} 67 68} 68 69 public static void main (String [] args) {69 70 reName (); 70 71} 71 72} 72 73 73View Code
This java program is mainly to learn how to understand the File during java IO operations, and then to write a program that replaces the characters.