Lab 6: File IO #1

This is a modification of programming project #5 at the end of chapter 10 in your textbook. The program you write shall modify a file by removing all duplicated space characters from it. As an example, suppose the file contained:

Now is  the time     for all
good   people    to come to the
aid of   their  computers.  

When your program is run, the file should then consist of:

Now is the time for all
good people to come to the
aid of their computers.  

To do this, you will want to have two files open: one containing the original text (with the duplicate space characters), and another where you will output the text from the first file, but without duplicated spaces. Other whitespace, such as newlines and tabs, should remain. I.e., the number of lines in the output file should be the same as in the input file. Access the input file via a Scanner and a FileInputStream, as shown on page 591 (TextFileScannerDemo.java) of your text. Access the output file via a FileOutputStream and a PrintWriter, as shown on page 581(TextFileOutputDemo.java) of your text.

The Scanner will allow you to access your input file in multiple ways. You should consider using the next() and nextLine() methods.

Step 1

Start by using "input.txt" as the name of the input file and "noDuplicates.txt" as the output file. Your program should copy the contents of the input file into the output file without modification. This is very much like the code in Display 10.4 in your textbook (HasNextLineDemo.java).

WARNING!! Before your program terminates, you must close your output stream to force the output to be created. Use the close() method, as demonstrated in the text.

Step 2

Once this works, modify your program, so that instead of always using "input.txt" as the input file, ask the user for the name of the input file. See Display 10.11 (pg. 610), FileClassDemo.java, for how to make sure that the name the user provides is to an existing file.

Step 3

Once that works, modify your program to skip duplicate space characters when writing to the output file. There are many ways to do this, but here is one: Read the input file a line at a time. For each line (now in a String variable), make a copy of that String, but converting duplicate space characters into a single space character. You could do this by iterating through the input line, one character at a time, dealing with the space character(s) appropriately.

For full credit the name of the output file should be "noDuplicates.txt". (If you go on to complete the Extra Credit component of the assignment, this will not be required.)

Extra Credit (worth 25% of a lab assignment)

Step 4

Now, for extra credit, complete the remainder of the assignment as stated in the book. The difference between the extra credit and the first part of this assignment is that the program will modify the actual input file to have no duplicate space characters, rather than just creating a new file containing that content. Also, your program should not alter any other file when it runs. (Note that your solution to the first part of this assignment overwrites any file named "noDuplicates.txt" when it runs.)

To receive full redit you must use a temporary file to hold the non-duplicated-spaces contents, but you have to make sure that your program does not overwrite any existing files on your machine except for the specified input file. To do that, first, your program should make sure that the name of the (temporary) output file differs from any already on your system. It should do that by checking for the prexistence of a file named "noDuplicates.txt". If there is such, it should try the name "noDuplicates2.txt", if that doesn't work, try "noDuplicates3.txt", etc. (You've probably guessed that you'll want to use a loop to keep trying these different names until you find a one not already in use. Create the potential file names in a string variable within the loop.) Use the technique shown in Display 10.11, pg. 610, to find a non-existing filename. [Do not use the File class's createTempFile method. In a sense, you are being asked to write your own createTempFile method.]

Okay, so now that your program has a unique name for the output file, it can go ahead and generate the non-whitespace-duplicated version of the input file. It can then close the file.

Step 5

Now you want your program to delete the original input file, and then change the name of your output file to be the same as the input file. You'll have to use some of the other methods of the File class. See pp. 611-613 of the text.

Lastly, check that your code is adequatelydecomposed. We're not talking various stages of rot, but rather how the solution of a larger problem should be decomposed into the solutions of several sub-problms. In general, try to shoot for methods being relatively small (say, less than 30 lines of code, even smaller is better).  This usually requires that you use helper functions, thereby decomposing the larger solution. So for example, you might have created helper functions named "getFileName" and "writeToFile" and called those from main(). This decomposing of a process into smaller, well-defined pieces makes it much easier for others to read your code and understand your intent and design.

I remind you that your grade for the course is a weighted average across all work done in the course. Consequently, even if you receive 100% on all of your labs, any extra credit you get toward labs will also help your overall course grade; so if you only got a B+ on an exam, but aced all your labs, this extra credit will help your overall course grade.