-2

I am stuck with a problem, where I want to use a txt file, which contains email header details. Using Java I want to search a specific label occurrence in that text and print that label details.

------------------------EXAMPLE OF HEADER FILE-----------------

    Return-Path: <example_from@dc.edu>
    X-SpamCatcher-Score: 1 [X]
    Received: from [136.167.40.119] (HELO dc.edu)
    by fe3.dc.edu (CommuniGate Pro SMTP 4.1.8)


    with ESMTP-TLS id 61258719 for example_to@mail.dc.edu; Mon, 23 Aug 2004 11:40:10 -0400
    Message-ID: <4129F3CA.2020509@dc.edu>
    Date: Mon, 23 Aug 2005 11:40:36 -0400
    From: Taylor Evans <example_from@dc.edu>
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.1) 
Gecko/2002082Netscape/7.0
    X-Accept-Language: en-us, en
    MIME-Version: 1.0
    To: Jon Smith <example_to@mail.dc.edu>
    Subject: Business Development Meeting
    Content-Type: text/plain; charset=us-ascii; format=flowed
    Content-Transfer-Encoding: 7bit'

-----------------------------------XXX-----------------------

In example above there is a label called "subject". I want to print that label and its details,
for example: Subject:Business Development Meeting, in output.

bluevoxel
  • 4,068
  • 10
  • 35
  • 56
RoTTi
  • 5
  • 1

4 Answers4

0
try {
    BufferedReader reader = new BufferedReader(new FileReader("File.txt"));
    String s=null;
    try {
        while((s=reader.readLine()) != null)
        {
            if(s.contains("Subject"))
            {
                System.out.println(s.substring(12));



            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

    }
0

Please Find a very easy solution..

 import java.io.BufferedReader;
    import java.io.FileReader;


    public class FileReader1 {

            static public void main( String args[] ) throws Exception 
            {
                try(BufferedReader br = new BufferedReader(new FileReader("E:\\Software\\Jyoti_WorkSpace\\Test\\src\\sample4.txt"))) {
                     String line = br.readLine();
                    while (line != null) {
                       line = br.readLine();
                       if (line!=null){
                       String output[]= line.split(":");
                       if(output.length>1)
                       System.out.println(output[0] +"--> "+ output[1]);
                       }
                    }

                }
            }
    }

OUTPUT

Message-ID-->  <4129F3CA.2020509@dc.edu>
Date-->  Mon, 23 Aug 2005 11
From-->  Taylor Evans <example_from@dc.edu>
User-Agent-->  Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv
X-Accept-Language-->  en-us, en
MIME-Version-->  1.0
To-->  Jon Smith <example_to@mail.dc.edu>
Subject-->  Business Development Meeting
Content-Type-->  text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding-->  7bit'
0

Let's say You've edited a little Your txt file:

Return-Path: <example_from@dc.edu>
X-SpamCatcher-Score: 1 [X]
Received: from [136.167.40.119] (HELO dc.edu)
By: fe3.dc.edu (CommuniGate Pro SMTP 4.1.8)


With: ESMTP-TLS id 61258719
For: example_to@mail.dc.edu; Mon, 23 Aug 2004 11:40:10 -0400
Message-ID: <4129F3CA.2020509@dc.edu>
Date: Mon, 23 Aug 2005 11:40:36 -0400
From: Taylor Evans <example_from@dc.edu>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.1) Gecko/2002082Netscape/7.0
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: Jon Smith <example_to@mail.dc.edu>
Subject: Business Development Meeting
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit'

...and You know it's path. Than, You can do simple while iteration, cooperated with simultaneously checking if currently reading line begins with specific String value. For example You want to display a line, where it begins with "Subject:" text. In this case, look on the example below and rearrange it for Your needs.

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class SearchTxtDemo {

    public static void main(String args[]) {

        /* declare and initialize reader for reading values from the console */
        BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

        /* prompt text for the user so he/she can decide, value of which tag should be displayed */
        System.out.println("Number + ENTER for getting value of specific tag:");
        String[] tags = new String[] {"Return-Path [0]", "X-SpamCatcher-Score [1]", "Received [2]", 
                "By [3]", "With [4]", "For [5]", "Message-ID [6]", "Date [7]", "From [8]", "User-Agent [9]",
                "X-Accept-Language [10]", "MIME-Version [11]", "To [12]", "Subject [13]", "Content-Type [14]", 
                "Content-Transfer-Encoding [15]"};
        for (String i : tags) {
            System.out.println(i);
        }

        /* get value given by the user */
        int valueGivenByUser = -1;
        try {
            valueGivenByUser = Integer.parseInt(consoleReader.readLine());
            consoleReader.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        /* cut the value given by the user by the whitespace, e.g "space" */
        String[] searchForTag = tags[valueGivenByUser].split("\\s");

        /* declare reader for the file with Your data */
        BufferedReader filereader;
        try {
            /* initialize file reader */
            filereader = new BufferedReader(new InputStreamReader(
            new FileInputStream("D:/Eclipse Java 8/eclipse_workspace/SampleText/src/SampleText.txt")));

            /* read the file in the iteration manner, */
            /* until reader will reach the end of the file */
            String line;
            while ((line = filereader.readLine()) != null) {

                /* check if readed line begins with the tag chosen by the user */
                /* if it matches, than show whole readed line in the console */
                if (line.startsWith(searchForTag[0])) {
                    System.out.println(line);
                    break;
                }
            }
            filereader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
bluevoxel
  • 4,068
  • 10
  • 35
  • 56
0

Try this. This way you store the line Subject: Business Development Meeting in string subjectDetails.

Scanner in = null;
string subjectDetails;

try {
    in = new Scanner(new FileInputStream("file.txt"));//use w/e source for input  
} catch (FileNotFoundException e) {
    System.exit(0);
    e.printStackTrace();
}

while(in.hasNextLine(){
    String line = in.nextLine()
    if(new Scanner(line).next().equals("Subject:"))
        subjectDetails = line;
}

Note that if you specify the file like that new FileInputStream("file.txt") then the file has to be placed in your project directory. Otherwise you can specify an absolute path for it.

Hope this helps

qbit
  • 2,528
  • 2
  • 12
  • 27