Laboratory 8

Exercise 1

Which Java class would be used to read known positions closer to the final of a big file? Justify.

Exercise 2

How to write into a file without losing its content? Justify your answer and write a scketch of the Java code, writing at the end of a file "Add this text at the end of the file!". Answer this question in two alternative ways.

Exercise 3

How the following code can achieve a better performance? Justify and provide a scketch of the code in Java.

int i;
Reader in = new FileReader("teste.in");
while((i=in.read())!=-1)
          System.out.print((char)i);

Exercise 4

Implement in Java two classes CountCharReader and CountCharWriter, to read/write characters from/to files and that simultaneously counts the number of times that a specified character is read/written. The specified character is received when the stream is created. Wrote a program Counter, with a main method, to test the implemented classes.

Suggestion: Use filter stream classes to add functionality to basic reader/writer streams.

Exercise 5

Create a project CardReaderWriter, inside this project create a new package called cardreaderwriter, and add to it the Card class (available within the description of this exercise) and change it in order to be serializable.

1. Create an instance of Card, print its value to the terminal, and serialize the corresponding object to a file. The print and serialization should be done within a method with the following signature:

void writeCard(Card card, String file)

where the card parameter is the object of type Card to serialize and the file parameter identifies the filename to where the card object should be serialized. An example of a card printing would be:

Writing card: Ace of Spades.

2. Read the serialized Card object and print its value to the terminal. The load and print of the serialized object should be done inside a method with the following signature:

Card readCard(String file)

which must return the loaded Card object and where the file parameter identifies the filename to where the card were serialized. An example of a card printing would be:

Reading card: Ace of Spades.


    Attachments