Question

In: Computer Science

Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named...

Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to run your program. java Exercise12_29 * can you make a example directory with sample files.. Please type you answer. Thanks

Solutions

Expert Solution

RenameFiles.java:

import java.io.File;

public class RenameFiles {

   public static void main(String[] args) {
       if(args.length != 0) {
       for(String name: args) {
       File f = new File(name);
if(f.isFile()){
String newName = getNewName(f.getName());
if(newName != null) {
       File newFile = new File(newName);
   f.renameTo(newFile);
}
}
       }
       }
   }

   private static String getNewName(String name) {
       String tokens[] = name.split("_");
      
       // if string is ending on _ without any digit at last
       if(name.charAt(name.length()-1) == '_') {
           return null;
       }
      
       // if string has got _ then, there will be atleast two parts
       if(tokens.length >= 2) {
           String fullName = "";
           int i=0;
           while(i < tokens.length-1) {
               fullName += tokens[i] + "_";
               i++;
           }
          
           // if last token is single digit
           if(tokens[tokens.length - 1].length()==1) {
               if(Character.isDigit(tokens[tokens.length - 1].charAt(0))) {
                   fullName += "0" + tokens[tokens.length-1];
                   return fullName;                  
               } else
                   return null;
           } else
               return null;
       }
       return null;
   }
}



Sample Output:



Program can be run as : java RenameFiles * as shown in the above image. It changes the files names as expected.


Related Solutions

Object-Oriented Design and Patterns in Java (the 3rd Edition Chapter 5) - Create a directory named...
Object-Oriented Design and Patterns in Java (the 3rd Edition Chapter 5) - Create a directory named as the question number and save the required solutions in the directory. - Each problem comes with an expected tester name. In the directory for a given problem, including the tester and all java classes successfully run this tester. Exercise 5.2 ObserverTester.java - Improve Exercise 1 by making the graph view editable. Attach a mouse listener to the panel that paints the graph. When...
You have an Active Directory forest named csmtech.local and two Active Directory domains in the forest...
You have an Active Directory forest named csmtech.local and two Active Directory domains in the forest named csmpub.local and csmsales.local. You want the DNS servers in each domain to be able to handle DNS queries from client computers for any of the other domains. DNS servers in the csmtech.local and csmpub.local domains should be authoritative for their own domains and the csmsales.local domain. However, DNS servers in csmsales. local should be authoritative only for csmsales.local. How should you set up...
Consider that you want to rename all files of a given type (e.g. file extension) to...
Consider that you want to rename all files of a given type (e.g. file extension) to a different filetype. For example, you want to change the file extension of all txt files to text. Write a shell script rename.sh (3%) that: takes two options as arguments. The first argument is the filetype you want to change. The second argument is the filetype you want to change files into. check that the two arguments are actually given. If not, the script...
JAVA There is a folder named Recursive folder at the same location as these below files,...
JAVA There is a folder named Recursive folder at the same location as these below files, which contains files and folders. I have read the data of the Recursive folder and stored in a variable. Now they are required to be encrypted and decrypted. You may copy past the files to three different files and see the output. I am confused on how to write code for encrypt() and decrypt() The names of files and folders are stored in one...
Complete Critical Thinking Activity 3: Configuring Zones. You have an Active Directory forest named csmtech.local and...
Complete Critical Thinking Activity 3: Configuring Zones. You have an Active Directory forest named csmtech.local and two Active Directory domains in the forest named csmpub.local and csmsales.local. You want the DNS servers in each domain to be able to handle DNS queries from client computers for any of the other domains. DNS servers in the csmtech.local and csmpub.local domains should be authoritative for their own domains and the csmsales.local domain. However, DNS servers in csmsales. local should be authoritative only...
In the funcs directory create a file named funcs.py. This section requires that you implement and...
In the funcs directory create a file named funcs.py. This section requires that you implement and test multiple functions. You should develop these functions and their tests one at a time. The function implementations must be placed in funcs.py. The test cases will, of course, be placed in the provided funcs_tests.py. You must provide at least two test cases for each of these functions. In addition, you should separate your testing into multiple functions (the file includes stubs for the...
Unix ramdom question. How do you display a list of files in your current directory, sorted...
Unix ramdom question. How do you display a list of files in your current directory, sorted by file size with the largest files at the top.
Write an inventory program in java for a used car lot. You should have one Car...
Write an inventory program in java for a used car lot. You should have one Car parent class and child classes for 3 vehicle types (convertible, SUV, Classic, etc). The parent class should have 3 variables and 3 functions, each child class should have 1 variable unique to it and 1 function. Your program should allow the user to store up to 100 cars in the inventory and write the cars out to a file when done.
You are to write a class named Rectangle. You must use separate files for the header...
You are to write a class named Rectangle. You must use separate files for the header (Rectangle.h) and implementation (Rectangle.cpp) just like you did for the Distance class lab and Deck/Card program. You have been provided the declaration for a class named Point. Assume this class has been implemented. You are just using this class, NOT implementing it. We have also provided a main function that will use the Point and Rectangle classes along with the output of this main...
I need this in java on textpad. There are two files, both have instructions in them...
I need this in java on textpad. There are two files, both have instructions in them on what to add in the code. They are posted below. public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT