muhkanda
muhkanda

Reputation: 349

How can I read and split data from file but Ignoring some data too in Java

I don't know how to explain this, but I have data like

1   John    US  38  24  090921
2   Dave    UK  29  12  043721
3   James   US  31  87  823874

The data separated by tab and I just want to read my data like this

1   John    US  38
2   Dave    UK  29
3   James   US  31

How can I do like that in Java? can you show me the sample of codes too? Thank You.

Upvotes: 2

Views: 99

Answers (1)

abdeali004
abdeali004

Reputation: 473

I think this will help you in your case.

BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader("filename"));
    String read = null;
    while ((read = in.readLine()) != null) {
        String[] splited = read.split("\\s+");
        for (String part : splited) {
            System.out.println(part); // to read the data is inputted correctly or not
        }
    }
} catch (IOException e) {
    System.out.println("There was a problem: " + e);
    e.printStackTrace();
} finally {
    try {
        in.close();
    } catch (Exception e) {
    }
}

Upvotes: 1

Related Questions