Last modification: 07/11/16
Securely delete file with random data.
Make a program Swipe
that takes a filename as argument, and overwrites its contents with a number of random bytes. The number of bytes are also taken as argument of the program.
Things can go wrong during the overwriting. Handle the java.io.FileNotFoundException
by printing some user-friendly message. Other exceptions can arise, so specify that your main()
method may throw an java.io.IOException
.
Make sure the file is closed in every cases.
Hint. Use a java.util.Random
object to generate random data.
Average of grades.
Create a program that computes the average of grades of a given student. The program takes the name of the student and a filename as arguments. The grades are read from the file. Every line consists of a name and equal number of grades, however the number of grades is not known in advance. We assume all lines are in correct format.
Example:
$ cat grades.txt
Brian:4 3 5 4 5
Peter:4 2 5 1 3
Adam:5 4 5 3 4
$ java Avg "Peter" grades.txt
3.0
$ java Avg "Smith" grades.txt
$ java Avg "Adam" asd
File not found!
Handle the java.io.FileNotFoundException
by printing some user-friendly message.
Subsequence.
Create a program that reads integers from a file until the sum of the integers reaches a given bound. Then it prints the subsequence read so far. The upper bound is taken as argument.
The file may contain arbitrary many integers.
Example:
$ cat nums.txt
1
2
4
0
-1
3
2
$ java Subseq 10
[1, 2, 4, 0, -1, 3]
Hint. You could use the toString()
method of a java.util.ArrayList
or a java.util.ArrayList
.