By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Create, Read and Write in Files - Streams in Java

+6 votes

Create a program that reads a log about users that spent time playing some game. The format is {user hh:mm hh:mm ... hh:mm}. Calculate the total time spent for each user and write it to another file total-played.txt

Java streams output - java task

asked in Java category by user john7
edited by user golearnweb

1 Answer

+2 votes
Best answer

Here is my solution and suggestions:

Step 1. Read and Process the Input

Reading the file info in java is easy. The BufferedReader class provides a handy method .readLine() that acts just like the Scanner’s .nextLine(). We are ready to read the first line. It’s a String, so save it in a variable of that type.

This will read the first line, but there may be many more. Since we don’t know the exact amount of lines, we can use a while loop.

Step 2. Calculate the time spent

Each of the lines can be split be space. The first element of the resulting array is the username, the next elements will be the individual sessions. Each session can be split by “:”. The left part is the hours; the right part is the minutes. You can keep the total time as minutes and then do some calculations.

Step 3. Write the output to another file

You need to create another file where the total time spent will be stored. You can use another constant. The FileWriter class will handle the output stream and will create the output file if it does not exist. The PrintWriter is a helper class with user-friendly methods. We will use the .println()
Step 4. Print the Result

In the while loop, after reading from the users file and calculating the time, print the next line to the output file.
Step 5. Close all resources

Finally, close all open streams

The solution:


public class JavaStreamsExercises {

    public static void main(String[] args) throws IOException {
        File users = new File("resources/users.txt");
        File totalPlayed = new File("resources/total-played.txt");

        BufferedReader reader = new BufferedReader(new FileReader(users));
        BufferedWriter writer = new BufferedWriter(new FileWriter(totalPlayed));
        String line = reader.readLine();

        while (line != null) {
            String[] lineArray = line.split(" ");
            String username = lineArray[0];

            int totalMinutesPlayed = 0;

            for (int i = 1; i < lineArray.length; i++) {
                String[] time = lineArray[i].split(":");

                int hours = Integer.parseInt(time[0]) * 60;
                int minutes = Integer.parseInt(time[1]);
                totalMinutesPlayed += hours + minutes;
            int totalMinutesPlayedToCount = totalMinutesPlayed;
            int minutesInDay = 1400;
            int daysPlayed = totalMinutesPlayedToCount / minutesInDay;
            totalMinutesPlayedToCount %= minutesInDay;
            int hoursPlayed = totalMinutesPlayedToCount / 60;
            totalMinutesPlayedToCount %= 60;
            int minutesPlayed = totalMinutesPlayedToCount;

            String outputToWrite = String.format("%s %d (%d days, %d hours, %d minutes) \r\n", username, totalMinutesPlayed, daysPlayed, hoursPlayed, minutesPlayed);

            line = reader.readLine();



answered by user nikole
selected by user golearnweb