Вы находитесь на странице: 1из 4

Homework Assignments - Week 9.

1 (files)

1. Greeting with Properties file


Write a greeting app which remembers the user:
- it should display a message like: "Hello, [user name], nice to see you again! (last visit
time: [h:m:s])”
- it will use a properties files (named 'user.properties') to store the username and the time
of last visit
- it will first try to find the file and load the 2 expected keys from it:
- if keys are loaded, just use them
- if file/keys are missing, ask the user for his name, and then use that one (also
saving it to the properties file)
Note: the program should ask the user for the name only once (when missing, and save it to
prop file); but it needs to update the time of the last visit on each run.

2. File Info
Given the path of a file, print this info about it:
- if file exists
- its absolute path, type (dir or file), size and last update date
- if it's writable
Test it then with some absolute and some relative paths (even something like “.”)

3. Folder contents
Given the path of a directory:
- list its direct descendants (files+dirs):
- should display one line for each, with the name and size
- list should be sorted descending by size
- OPTIONAL: place the directories first and sorted ascending by name (and after
them the files descending by size)
- display the total size of all files
4. Find file (by name)
- Read from user:
- the path of a base directory
- a file name pattern to search for (a String value)
- Search under that base dir (and ​all its subfolders!​) for a file/dir which contains in its name
the given pattern (case insensitive)
- You should stop and return only the first file found (we don't care if there are multiple
matching files), and display its full path, or display another message if no file was found

Example:
- Given base path: ".\doc":
- for pattern: "week8_TE" it should find and display the path of this file (if started
from this project, like: "C:\....\doc\s4_week8_tema.txt")
- for pattern: "week9_" it should display a message like: "No files found matching
pattern: 'week9_'"

BONUS:
- if a file was found, show more info about it: name, size, if it's a dir or not (how can you do
that in a more optimal way, what could you change in the signature of your recursive
function?)
- show all the matching files, not just the first one (show full info for each, not just name)

5. Info on all files


Given the path of a base directory, scan all the file tree under it (including all levels) and:
a. Display the total number of files and directories under it
b. Find and show the top 5 biggest files (display for each file the path and size; list should
be sorted desc by size)
c. Write the previous displayed info to a new text file (placed under base dir, named
'dir_info.txt')

Hint:​ this is a good place to use RECURSIVE methods:


- In general, your recursive method should receive as input a directory path and return
some value (total files count, or biggest file, or a list of files..) computed for that whole
tree; it will also call itself as needed on the smaller trees under the current dir (with a
stop condition when the received tree has no more levels, etc)
- See ListDirectoryTreeExample.java for something similar. Also, to better understand
recursion, read from: /doc/books/thinkjava.pdf, chapters 5.8, 5.9!
6. Reading file lines
Given a file path, read its content (hint: may use Scanner with .nextLine()) and then:
a. Display first N lines (N read from console), each line being prefixed by its line count

b. Find and display the longest line (including its length)


i. Optional: also include the line number for this longest line

c. Display these statistics:


i. number of lines, number of words (hint: for splitting a line to words may use
String.split() method + some regular expression, see RegExExample.java..)
ii. show the list of all words (sorted alphabetically) together with the usage count for
each;
■ hints:
● store them in a kind of Map<String, Long>;
● may also use Streams with a grouping collector;
● when counting words usage you should ignore the different casing
iii. OPTIONAL: top 5 most used words (hint: use a TreeMap<String,Long> to store
each word+count, and create a custom comparator which sorts by count - and
use an instance of this comparator when creating the TreeMap, so its entries will
be automatically sorted by it..)
iv. the longest word

d. Write these statistics to a new text file (named like initial file + '_statistics.txt' suffix)

Hint: for reading the lines of a file, you may use:


- a Scanner instance (calling methods .hasNext(), nextLine)
- Files.lines() to read all lines of a file as a Stream<String>

7. Persons from CSV file


You are given a text file in CSV (Comma Separated Values) format, which contains data about
a list of persons (persons.csv).

File format:
- it doesn't have a separate/special header row
- all rows represent persons, having the same format: ​[name],[cnp],[age],[height],[sex]
- expected field types:
- name - a String
- cnp - a long number
- age, height - a positive integer value
- sex - one of these 2 values: 'F'/'M'

Write a program which should:


- load all rows from file, skipping the invalid ones (invalid rows like: having less fields than
expected, or with values of different types than expected -> you could also show an error
message for each ignored row)
- display the number of valid records found
- find the oldest person and display some info about it (name+age)
- display the list of all (valid) persons, sorted ascending by name
- save this sorted list to a new csv file, with same format as initial one (named
'persons_sorted.txt')

​Hint​: you could organize your code in a few separate classes:


- Person (with all fields for it/equals()/toString()..),
- PersonComparator (for sorting by name),
- PersonWriter and PersonReader (for writing a List<Person> to a CSV file and for
loading such a list from it).
Then manually test the writer and reader classes to make sure they work ok, and then
start using them for rest of assignment..

Вам также может понравиться