Hello everybody.
I have been studying Java.
I have been using the book "Introduction to Java Programming Comprehensive Version 10th edition by Y Daniel Liang".
I'm using the IDE NetBeans 8.2.
I was solving the exercise 12.28 in the end of the Chapter 12.
This is the text of that exercise:
(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.
java Exercise12_28 *
My First Question is about this Supplement III.V.
I never found this Supplement III.V in the internet and I don´t know how to solve this exercise using the symbol from the command line to get all the files in the directory.
Instead, I solved this exercise using the listFile() method in the File Class to get an Array of all the files.
I have created a Java Package for the java Files in:
package Chapter12.EndofChapter12.Exerc12dot28;
and I created a Java Package for the text Files that would have their file names changed in:
package Chapter12.EndofChapter12.Exerc12dot28.Text Files
Also, I have used the following path in my Java Programs:
path = "src/Chapter12/EndofChapter12/Exerc12dot28/Text Files";
Once I have an array of Files, I iterate and get Each File Name.
Because in the Text Files there are Files named "Exerc12dot27" and "Exerc12dot28", I checked for match with Regular Expression:
"Exerc12dot28.+";
and also if the name ends with ".txt"
The matching files would be added to an Array List of Files.
The text files have names like:
"Exerc12dot28Exercise1_1"
and
"Exerc12dot28Exercise10_10"
So then I needed to iterate in the Array List and for each file, get the name.
Then I needed to search for
"Exercise" and "_"
Then I would get the value of i
if(fileI > 0 && fileI < 10)
then I would pad the 0 before i
exerciseI is the initial value of i
String padZero = "0";
String newFileI = padZero + exerciseI;
String newExerciseSubString = "Exercise" + newFileI + SameJ
newFileName = currentFileName.replaceFirst(
currentFileSubstringReplace, newExerciseSubString);
With the code before when I Rename the File the File is renamed and stays in the same folder
And this is where I had the second problem, because I initially tried a different Rename and the File was removed from where it was and added to the Current Working Directory.
These are the two comments that I tried before:
/*
File fileRename = new File(newFileName);
//This moves the files to the Current Work Directory
*/
/*
File fileRename = new File(path + newFileName);
//This moves the files to the Exerc12dot28 Source Package
//and renames as Text Files + OriginalFileNameChanged
*/
So my second question is if someone can explain that the files are renamed and moved to a different folder.
I use a computer with Windows and when I rename a Folder or a File, it stays in the same Folder.
I wish you a good weekend.
Thank you,
Rogério