|
| 1 | +package ch_12.exercise12_29; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.nio.file.StandardCopyOption; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +/** |
| 15 | + * **12.29 (Rename files) Suppose you have a lot of files in a directory named Exercisei_j, |
| 16 | + * where i and j are digits. Write a program that pads a 0 before j if j is a single |
| 17 | + * digit. For example, a file named Exercise2_1 in a directory will be renamed to |
| 18 | + * Exercise2_01. In Java, when you pass the symbol * from the command line, |
| 19 | + * it refers to all files in the directory (see Supplement III.V). Use the following |
| 20 | + * command to run your program. |
| 21 | + * To test: |
| 22 | + * rename files in ./testDir by removing the '0' padding in the suffix for j. |
| 23 | + * <p> |
| 24 | + * (From inside: java-prog-dan-lang-10th/src/ch_12/exercise12_29/ ) cmd: javac Exercise12_29.java |
| 25 | + * cmd: cd ../.. |
| 26 | + * cmd: java ch_12.exercise12_29.Exercise12_29 * |
| 27 | + */ |
| 28 | +public class Exercise12_29 { |
| 29 | + private static final Pattern PATTERN = Pattern.compile("Exercise\\d{1,2}_\\d.*"); // |
| 30 | + |
| 31 | + public static void main(String[] args) { |
| 32 | + |
| 33 | + if (args.length < 1) { |
| 34 | + System.out.println("Usage: java Exercise12_29 *"); |
| 35 | + return; |
| 36 | + } |
| 37 | + if (!args[0].equals("*")) { |
| 38 | + System.out.println("Usage: java Exercise12_29 *"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + File srcDirectory = new File("./testDir"); |
| 43 | + if (!srcDirectory.isDirectory()) { |
| 44 | + System.out.println("The directory path is not a directory: " + srcDirectory.getAbsolutePath()); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (!Objects.isNull(srcDirectory.listFiles()) && srcDirectory.listFiles().length > 0) { |
| 49 | + ArrayList<File> files = new ArrayList<>(Arrays.asList(srcDirectory.listFiles())); |
| 50 | + for (File f : files) { |
| 51 | + String fileName = f.getName(); |
| 52 | + StringBuffer stringBuffer = new StringBuffer(fileName); |
| 53 | + if (PATTERN.matcher(stringBuffer.toString()).matches()) { |
| 54 | + stringBuffer.insert(fileName.lastIndexOf("_") + 1, "0"); |
| 55 | + System.out.println("Renaming: " + fileName + " to " + stringBuffer.toString()); |
| 56 | + Path srcPath = f.toPath(); |
| 57 | + Path trgtPath = Paths.get(srcDirectory.toPath().toString(), stringBuffer.toString()); |
| 58 | + try { |
| 59 | + Files.move(srcPath, trgtPath, StandardCopyOption.REPLACE_EXISTING); |
| 60 | + System.out.println("Rename successful"); |
| 61 | + } catch (IOException ioe) { |
| 62 | + System.out.println("Something went wrong while renaming the file... " + fileName + "\n\n"); |
| 63 | + ioe.printStackTrace(); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + } else { |
| 70 | + System.out.println("The current directory is empty..."); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | +} |
0 commit comments