Pages

Thursday, February 17, 2011

Lab Excercises
Software Programming-II Lab
Topic: Using IO package classes
 

1.   Use of mark and reset methods of InputStream:  
Write a program which reads characters from a text file and searches for the first occurrence of letter ‘A’. It remembers the position at which ‘A’  occurs by marking it with a read limit of 15. It searches the next fifteen characters for the occurrence of ‘B’. If ‘B’ occurs within 15 characters it resets back to ‘A’s position and prints the characters in between ‘A’ and ‘B’.

2.      File Copy Program:
Write a program which takes a two file names from the command line argument and opens the first file and copies it’s content to the second file.
(Use classes: FileInputStream, FileOutputStream).

3.      Dealing with binary data:
Create a program to read a list of floating point numbers in a text file and then write the data to a  binary file.
 (Use classes: FileReader, FileBufferedReader, FileOutputStream, String, Float, DataOutputStream).
Note
Use FileReader and FileBufferedReader classes to be able to read each line of the file as a string. Use split method of String class to break the line into words.
Use Float.parseFloat to convert the string to floating point number. Use FileOutputStream and DataOutputStream to write the float data as a binary data to the file.
4.      Our own Serialization: Assume that ComplexData.txt contains the data to build
five complex objects. Write a program which reads this data and constructs five
complex objects. It then finds the sum of the five complex objects to create a sixth
object. It stores the data of the sixth complex object in a output text file. (Use
Classes: Complex, FileReader, FileBufferedReader, FileOutputStream, String,
Float, DataOutputStream).
Note: Use the technique of 3rd problem to read the text data as binary data.
5.   Incorporation of Metadata:
Assume that ComplexData.txt has the following form.

3
2.2  4.5
4.6  7.9
9.0  3.0
That is the first number gives the number of Complex objects and the remaining
data is the data required to construct them. Modify the program of Q.No: 4 to
adapt to this change.
6.      Write your own ls -R program: Write a program which takes a directory name from the command line argument and prints the contents of the directory hierarchically. (i.e. If the directory contains another directory it’s contents are shown recursively). (Use Classes: File).

---> TIPS
1) Knowing the end of the file when reading data from file: Normally when reading
byte by byte from the file end of file can be denoted by return value of -1 from the read system call. But when you are reading binary data using methods such as readInt and readDouble in DataInputStream class another technique is employed. Whenever Eof is reached an exception called as EOFException is generated. You can catch this exception to know the occurrence of the end of file. Assume that you are reading one integer and one double alternatively from a binary file the code looks as follows.


while (true) {
  try {
       i_data = data_in.readInt ();
      d_data = data_in.readDouble ();
 }
  catch (EOFException eof) {
    System.out.println ("End of File"+eof);
    break; //End of file reached break from the while loop
 }
} //End of while loop

2) Catching of IOException: Most of the methods dealing with I/o generates IO
exception. Hence surround that code in a similar try catch block shown earlier to catch IOException.

Good code is its own best documentation. As you're about to add a comment, ask yourself, 'How can I improve the code so that this comment isn't needed?' Improve the code and then document it to make it even clearer.
 -- Steve McConnell, software engineer and author, from Code Complete