This is the 3rd try for Chapter 12 Exercise 28 Java I have this code /**********
ID: 3804873 • Letter: T
Question
This is the 3rd try for Chapter 12 Exercise 28 Java
I have this code
/*********************************************************************************
* (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 i *
* if i is a single digit. For example, a file named Exercise2_1 in a directory *
* will be renamed to Exercise02_1. 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. *
*********************************************************************************/
package Renamefiles;
import java.io.File;
public class Rename_Files {
public static void main(String[] args) {
renameFiles(args); // renaming filesnames that are passed as arguments on command prompt
}
private static void renameFiles(String[] fileNames) { //Array fileNames has all files to be renamed.
for (String fileName : fileNames) { // for each fileName in fileNames array, rename if such file exist.
if (fileName.matches("Exercise\d_\d+\.\w+")) {
// if fileName has the format given in problem then only rename it
StringBuilder stringBuilder = new StringBuilder(fileName);
//Create a StringBuilder for fileName so that it becomes mutable.
stringBuilder.insert(fileName.indexOf('_') - 1, "0");
// insert 0 before one position of '_' symbol in fileName stringBuilder.
new File(fileName).renameTo(new File(stringBuilder.toString())); //perform rename operation on file
}
}
}
}
I have Exercise1_1.txt Exercise1_2.txt Exercise1_3.txt in the same folder as the Rename_Files.java
and I made a package Renamefiles that uses this code. I just cannot get the command prompt to come up and the code will not rename the files.
I am using jGRASP CSD java
G Works Rename File Rename Files,java c: UsersArrrablDesktopWavalcom comuevelupwava exercises Renamefiles jGRASP csD Java) Eile Edit View Build Project Settings Tools Window Help Rename Filesiava C UsersvrroblDesktopyavalcomicominlevelup yavalexercisesWRenamefiles will be ren anted to Exerciae02 1 In Java When you paaa the a from the L Exercise txt 1-1 2.txt Use the following c run your program. Exercise 3.txt 2-1 txt Exercise Exercise 2 txt package Renamerilea Exercise 3txt import Java-io .File Rename Files class. Rename Files java public class Rename Files public static void main (3tring C] args) renami. rename Filea larga ng file anamea that are paaaed aa argumenta on command prompt private static void. rename iles (String rileNames) f //Array fileName a has all rile e to be renamed String fileName for each fileName in fileNames array e if auch file exist for Ald W.Ww+ if fileN matches ("Exercise Ald 11 fileName haa the format given 1n problem then nly rename St g Build g lder new String Builde fileN //Create a stringBuilder for fileName ao that it become a mutable Open Projects string Builder-insert (fileName indexof 1, "0"): insert 0 before ne position of ymbol in filellame stringBuilder //perform r file new File (11.1 OiName renameTo (nehr File latringBuilder .tostring e operatio Renamefiles CAUsersirrobDesktopiavalcomicomuevelupiavalexercise Compile Messages GRASP Messages Run vo Interactions ----jGRA3P operation complete. End JGRASP e ave Re files e Files Clear JGRASP operation complete GRASP exec ava Rename f1lea Filea GRASP: operation complete Browse ind Debug Workbench Line: 21 Col30 Code:40 op:4. 5 XExplanation / Answer
Try to compile and excute the program using command prompt is Windows OS.
Use "javac" and "java" commands to compile and excute using command line arguments.
Modify your code to haldle if the entered argument is a file or folder to avoid runtime crahses/exception.
Let me know in case you have any doubt.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.