Home > database >  Java: Extract data from a File
Java: Extract data from a File

Time:01-22

package Testing;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class testing {

    // map to store the number of errors per user
    private static Map<String, Integer> errorsPerUser = new HashMap<>();

    // variable to store the number of jobs started
    private static int jobsStarted = 0;

    // variable to store the number of jobs completed
    private static int jobsCompleted = 0;

    public static void main(String[] args) {
        // specify the path to the log file
        String filePath = "C:/Users/Wafiq/Documents/WIX1002/GroupAssignment/extracted_log.txt";

        try (Scanner scanner = new Scanner(new File(filePath))) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                int timestampEndIndex = line.indexOf("]");
                String lineWithoutTimestamp = line.substring(timestampEndIndex 2);
                // check if line contains error message
                if (lineWithoutTimestamp.contains("error: This association")) {
                    // extract the user from the line
                    String user = extractUser(lineWithoutTimestamp);
                    // increment the error count for the user
                    incrementErrorCount(user);
                }
                // check if line indicates job start
                if (lineWithoutTimestamp.contains("sched: Allocate")) {
                    jobsStarted  ;
                }
                // check if line indicates job completion
                if (lineWithoutTimestamp.contains("_job_complete: JobId")) {
                    jobsCompleted  ;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // print the results
        System.out.println("Number of jobs started: "   jobsStarted);
        System.out.println("Number of jobs completed: "   jobsCompleted);
        System.out.println("Number of errors per user:");
        for (Map.Entry<String, Integer> entry : errorsPerUser.entrySet()) {
            System.out.println(": "   entry.getValue());
        }
    }

    // method to extract the user from the line
    private static String extractUser(String line) {
        // assuming the user is the string before "error" in the line
        return line.substring(0, line.indexOf("error")).trim();
    }

    // method to increment the error count for the user
    private static void incrementErrorCount(String user) {
        if (errorsPerUser.containsKey(user)) {
            errorsPerUser.put(user, errorsPerUser.get(user)   1);
        } else {
            errorsPerUser.put(user, 1);
        }
        
    }

    
}

Output:

Output

File data:

File data

I'm trying to extract the number of jobs causing error and the corresponding user. I have done the number of jobs causing error but I don't know how to extract the number of corresponding user.

(p/s: Pls don't slander me, I'm a first year student in Comp Science. I have tried my best)

The user is not at the same index each line so I dont know how to extract it from the line.

CodePudding user response:

While the user is not at the same index across lines, it always comes after user=' and ends on the next '. Search for these substrings in your line and you are done.

int startIndex = line.indexOf("user='");
if (startIndex>=0) {
    int endIndex = line.indexOf("'", startIndex);

    String user = line.substring(startIndex, endIndex);
    System.out.println("user=" user);
} else {
    System.out.println("no user in line");
}

Edit: I saw there is another pattern also in use. I think you can change the above algorithm to also allow for the second one.

CodePudding user response:

In Java, you can extract data from a file using the File and Scanner classes. The File class represents a file in the file system, and the Scanner class allows you to read data from a file.

Here is an example of how to extract data from a file named "example.txt" and print it out:

import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try {
            File file = new File("example.txt");
            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        }
    }
}

Alternatively, you can use java.nio.file package for reading the file.

import java.nio.file.*;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        Path filePath = Paths.get("example.txt");
        try {
            byte[] fileBytes = Files.readAllBytes(filePath);
            String fileContent = new String(fileBytes);
            System.out.println(fileContent);
        } catch (IOException e) {
            System.out.println("File not found");
        }
    }
}

Both of the above examples assume that the file "example.txt" is located in the same directory as the Java program.

In both of the examples, the try-catch block is used to handle the FileNotFoundException that can be thrown if the file is not found.

  • Related