Question

I have this tag

<META HTTP-EQUIV="Expires" CONTENT="Thu, 23 Aug 2012 09:30:00 GMT">

in the file. I have to find this tag in file and get the content part out of it and match with the current date and time. If date and time in the file is older than current time set a flag. Can somebody help me to do this, I am new to this? Thanks

Was it helpful?

Solution

This here might help you getting started:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class FindStringInFileTest {

    public static void main(String[] args) {

        File f = new File("c:/test.txt");
        String res = find(f);

        if (res != null) {
            System.out.println("Found Meta Http-Equiv tag");
            System.out.println(res);//print META HTTP-EQUIV line
            //check the line for whatver dates etc here
        } else {
            System.out.println("Couldnt find Meta Http-Equiv tag");
        }

    }

    public static String find(File f) {
        String result = "";
        Scanner in = null;
        try {
            in = new Scanner(new FileReader(f));
            while (in.hasNextLine()) {
                String tmp = in.nextLine();
                if (containsMetaHttpEquiv(tmp)) {
                    result = tmp;//assign line which has META HTTP-EQUIV tag
                    break;//so we dont check more
                } else {
                    result = null;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static boolean containsMetaHttpEquiv(String str) {
        if (str.contains("<META HTTP-EQUIV=\"Expires\" CONTENT=")) {
            return true;
        }
        return false;
    }
}

It will read in the text file and check for META HTTP-EQUIV tag and either return the line/string which contains the tag or null if no META HTTP-EQUIV tag could be found. you would then extract the date using substring() and indexOf() methods and then parse this to a SimpleDateFormat and then compare the 2 dates, and write flag appropriate flag to file.

EDIT:

Here is the method needed to extract the content from the HTTP-EQUIV META tag:

public static String getContentOfMetaTag(String tag) {
    String search = "CONTENT=";
    return tag.substring(tag.indexOf("CONTENT=") + search.length() + 1, tag.indexOf('>') - 1);
}

you would call this method with the String returned from find(new File) (make sure its not null before calling getContentOfMetaTag(String tag))

OTHER TIPS

  1. The content file seems like an HTML file. So use a library like JSoup to retrieve the value of the CONTENT attribute of META tag.
  2. Then use SimpleDateFormat to convert the string to a Calendar/Date object.
  3. Then use after()/before() API for comparison.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top