Join Julian Robichaux for an in-depth discussion in this video Structuring the code, part of Learning Java by Example.
- [Narrator] Before I write my classes, I like to start by sketching out the methods I'm going to need in order to make it work. For this application, we want to do the following, read all the files in a directory, optionally search each file to see if it matches a regular expression, and, then, optionally add each file that matches to a ZIP file. That seems pretty straightforward. Based on the list, our class will need, at a minimum, the following variables, a path string, a regular expression which is also a string, and the zip file name which is gonna be a third string.
Let's add those to our class. We might need some more variables than that depending on how we structure the logic of the rest of the app, but, for now, we can start with those. So, I'll create a string named path, a string named regex, and a string named zipfilename. We might need some more variables than that depending on how we structure the logic of the rest of the app, but, for now, we can start with those. Typical Java classes will have getter and setter methods for private variables that need to be accessed outside the class.
This class will be self-contained, but it's easy enough to go ahead and add the getters and setters in Eclipse, so let's do that. Just right-click anywhere in the source code and choose the menu options Source, Generate Getters and Setters. Click the Select All button to choose the private variables that we added, set the insertion point to After main, and click OK. Now all of your getters and setters are in place and Eclipse just saved you a lot of typing. So, you can see this just created the getPath method, the setPath method, getRegex, setRegex, getZipFileName, and setZipFileName.
Now we can add some code to the main method so it can process command line arguments and actually do something. So, first I'll delete this because we don't need this anymore. Then I'll create an instance of this class. FileSearchApp equals new FileSearchApp. Second, we process the command line arguments using a switch block. At the start of the switch block, I'm just making sure that we're only getting three command line arguments or less.
If someone decides they're gonna pass in five, seven, ten, 20 command line arguments, we can just ignore everything but the first three. Then I can go through the different case statements and determine what I need to do depending on how many command line arguments we got. In the case where we got zero command line arguments, I can just print out a usage statement. I'll type sysout, Control + space, and then I'll type USAGE, FileSearchApp, path, regex, zipfile.
After that, we can return because there's nothing else to do. In the case that we get three arguments, we know that the third argument is the zip file name. So, we can set that directly here. The second argument, if we have two arguments, is going to be the regular expression, and the first argument is always going to be the path. After this, we can actually tell the app object to do something. We don't have any methods that do anything yet, but we do know that the app will need to read all the files in the directory, so we can create a fake method called walkDirectory.
We don't know what that method will do yet, or how it's gonna work, but that's a good name and we can always surround it with a try-catch block, just to be safe. In general, the main method should do very little work and have very little code. The other methods in the class should be responsible for all of the code and application logic. The main method is just supposed to boot-strap to launch the app. In this case, we can't really avoid processing command line arguments in the main method because this is the only place that command line arguments could be passed. But, once we have the arguments, we just tell the File Search app object to do whatever it needs to do.
In the table we saw earlier, we listed the application functionality and the variables that went with that functionality. Now we can outline some methods that will actually perform these functions. I made some arbitrary decisions about the names of the methods and the parameters each one will take. We can change those later if we need to. For now, let's just add this walkDirctory, searchFile, and addFileToZip method to the code. I'm not gonna do anything in these methods yet, but I'll at least do a system.out.printline so we can see that we actually encountered these methods and that everything is working the way we think it should.
From the walkDirectory method, we're gonna call searchfile and addFileToZip. So, I'll put the placeholders in for that. And now create searchfile and addFiletoZip. Since our project doesn't have the file object imported yet, we can add that automatically by placing the cursor just after the word, file, pressing Control + Space, and then letting Eclipse find out what the different file options are.
Here we can choose the first one, Java.iO.file and press enter. That will automatically import the file object into the project and all of our red x's go away. Okay, let's test the code. Make sure you save everything with either the File-Save menu or by pressing Control + S, for save. On a Mac, it's going to be Command + S. By default, Eclipse has auto-build on, so every time you save your Java files they automatically get recompiled and they're ready to use. If you turn this off somehow, just choose the menu option, Project Build Project to compile your code.
Now, you can right-click your Java file and Package Explorer on the left, and choose the menu option Run As Java Application. This opens a console window and shows you the result of running the app. In this case, the console window is minimized so it shows up as an extra window in front of the code. If you click the Restore icon at the top-right side of the title bar, the console window will show up below your code, by default. In this case, we just got the USAGE statement because there was zero arguments passed to the command line.
If we want to add some arguments, just like we did before to run the app, right-click the Java file and navigate to the Run As menu, just like we did before. But this time instead of running the app, choose the sub-menu option, Run Configurations. This displays a dialog of Run Configurations for the project with the one that you just created highlighted. Go to the Arguments tab, and type some text in the Program Arguments box. One, two, three. Then click Apply and Run.
This time the console displays the results of running your Java application with the arguments you just typed. Now that we have the skeleton of our application in place, and we know how to test and run it, in our next section we can start filling in the methods with working code.
- Creating a command-line application
- Building a graphical user interface
- Creating a Java API
- Parsing JSON data
- Refactoring and testing Java code