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...
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...
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.
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 =...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Arrays Assignment in Java 1. Suppose you are doing a report on speeding. You have the...
Arrays Assignment in Java 1. Suppose you are doing a report on speeding. You have the data from 10 different people who were speeding and would like to find out some statistics on them. Write a program that will input the speed they were going. You may assume that the speed limit was 55. Your program should output the highest speed, the average speed, the number of people who were between 0-10 miles over, the number between 10 and 20,...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be using them. In your driver class: • Create an array called “boyNames” and store all names from the BoyNames.txt file • Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file • Create a text menu that allowing users to: 1. Enter a name and the application must display a message indicating if the name is among the most popular...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT