Ciara
Ciara

Reputation: 197

Java: How to read a specific line from a text file?

I have this text file:

2013001  Jaymarion Manalo L. Service Crew March 15, 2013   8:00   12:00   13:00   17:00   10.0
2013001  Jaymarion Manalo L. Service Crew March 16, 2013   8:00   12:05   13:05   17:30   10.0

Now, I have a condition that if String date1 = "March 15, 2013", it will read the line where it belongs and will only get the "8:00", "12:00", "13:00", and "17:00" and display it on the text fields I have declared. I mean, is that even possible? How is it done? I hope you're getting what I'm trying to say. I am so new at Java -_-

Upvotes: 2

Views: 22484

Answers (5)

A4L
A4L

Reputation: 17595

Ofcource this possible.

You need to read the file line by line. Check each line if it contains date1. If it does then extract and parse the data you want.

Here is a sample code how you can achieve this with standard api:

public class ReadFileLineByLineAnExtractSomeText {

    private static final String src = "test.txt";
    private static final String date1 = "March 15, 2013";

    @BeforeClass
    public static void genTextFile() throws IOException {
        OutputStream os = new FileOutputStream(src);
        os.write(("blah bla foo bar\n" +
                "2013001  Jaymarion Manalo L. Service Crew March 15, 2013   8:00   12:00   13:00   17:00   10.0\r\n" + 
                "2013001  Jaymarion Manalo L. Service Crew March 16, 2013   8:00   12:05   13:05   17:30   10.0\r" +
                "... the end").getBytes());
        os.close();
    }


    @Test
    public void testReadAndExtract() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(src));
        String line = br.readLine();
        int lineNumber = 0;
        while(line != null) {
            lineNumber++;
            int i = line.indexOf(date1);
            if(i != -1) {
                int s = i + date1.length();
                int e = line.length();
                System.out.println(date1 + " found in line " + lineNumber  + " at index " + i + ", extract text from " + s + " to " + e);
                String extractedText = line.substring(s, e);
                String[] extractedTextParts = extractedText.trim().split("\\s+");
                for(String part : extractedTextParts) {
                    if(isTime(part)) {
                        System.out.println("    '" + part + "'");   
                    }
                }
            }
            line = br.readLine();
        }
        br.close();
    }

    private boolean isTime(String part) {
        return part == null ? false : part.matches("\\d{1,2}:\\d{1,2}");
    }
}

Output

March 15, 2013 found in line 2 at index 42, extract text from 56 to 94
    '8:00'
    '12:00'
    '13:00'
    '17:00'

Upvotes: 1

1218985
1218985

Reputation: 8012

You could try this:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileReaderService {

    public static void main(String[] args) {
        FileReaderService fileReaderService = new FileReaderService();
        fileReaderService.performExecute("March 15, 2013");
    }

    public void performExecute(String inputPattern) {
        List<String[]> matches = new ArrayList<String[]>();
        try {
            FileInputStream fileInputStream = new FileInputStream("C:/Users/Guest/Desktop/your-file.txt"); /*change your file path here*/
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
            String strLine;
            while ((strLine = bufferedReader.readLine()) != null) {
                if (strLine.contains(inputPattern)) {
                    String[] splits = strLine.split("[\\s]{3,}"); /*splits matching line with 3 consecutive white spaces*/
                    matches.add(splits);
                }
            }
            dataInputStream.close();

            for (String[] items : matches) {
                System.out.println(items[1] + "\t" + items[2] + "\t" + items[3] + "\t" + items[4]);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Output:

8:00    12:00   13:00   17:00

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

As suggested you can iterate to the line which you want and then you can try something like this:-

String s= FileUtils.readLines(filename).get(lineNumber);

For more information on FileUtils.readLines() check this out.

Upvotes: 0

karan
karan

Reputation: 8843

to read line from line...

try {
    FileReader fileReader = 
        new FileReader(fileName);

    BufferedReader bufferedReader = 
        new BufferedReader(fileReader);
    //in any loop use bufferedreader.readline() untill you get your desired line
    //to part your string use split()
    bufferedReader.close();         
}

Upvotes: 0

Apurv
Apurv

Reputation: 3753

You can read file as a list of strings using FileUtils.readLines() This will give you a list of Strings. Then iterate over the list and look for the date using string.contains("March 15, 2013").

Note: I can share the complete code with you, but you should try to code with the above information.

Upvotes: 1

Related Questions