NB! The practical session exercises are always provided enough and to spare with an eye to fast / experienced students have exercises to solve. To this end, do not get upset if you do not get on all the task in class. Take your time and complete the tasks at home!
Task 1. Encoding
The main goal of the task is to test different encodings when the program reads from the file or writes into the file. First of all, invoke the method Charset.defaultCharset()
and print out the result.
Write a program which tests different encodings:
- The program has to write into the file the following sentence "Andja käsi väsis ärä, võtj käsi ei väsi kunagi". Do not set any encodings yet.
- Add the functionality of reading from the file into the program. This time set the encoding so that is differs from the default encoding of your computer (e.g. if the default encoding is windows-1257, use UTF-8). Run the program and explain the result.
- Change the program so that if the program writes into the file, a non-default encoding is used for writing. Do not set any encodings for the reading (use OS default encoding). Explain the output of the program.
Task 2. Passwords
Write a program which has a static method called findPsw
. The method has one parameter for the name of a file (String
). The method looks for passwords in this file and writes the passwords into another file - "psw.txt". Each password in the file consists of a single word placed after the keyword " parool: ". A sample file with hidden passwords is here: http://www.ut.ee/~eno/peidetud.txt (save the file on your computer). In the main method, invoke findPsw
.
Task 3. Buffered streams
Write a program which reads a word from the console and writes it into the file using the BufferedWriter
class. After that the program has to read the word from the file using the BufferedReader
class and output the word to the console. Use UTF-8 encoding. Note: do not use Scanner
.
Task 4. Person
Create a class called Person
. The class should have private instance fields for the first name (String
), the last name (String
) and the list of children's names (List<String>
). The class should also have a constructor which instantiates the instance fields.
In the client class, create an instance of the class Person
and write it into the file using the DataOutputStream
stream. Create one more instance of the Person
class and instantiate it with the data from the DataInputStream
stream.
Output the data before writing into the file and after reading from the file. Note: the number of children's names in the list is not fixed (the number of children should be added into the file in order to ease reading from the file).
Task 5. Close streams
The goal of the task is to check what happens if the file is not closed. Write a program which opens a file given in the console. Then the program has to ask the user for a joke using System.in
. Run the program and let it wait for the joke. In Windows File Explorer, try to rename the file. What happens? Change the program so that it closes the file before the user is prompted for a joke. Run the program and try to rename the file.