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 "Andje käsi väsis ärä, võtje 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 the passwords in this file and writes the passwords into another file - "psw.txt". Each password in the file consists of a single word placed followed 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 command line 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.
Task 4. 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 command line. 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.
Task 5. 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).