java中的文件处理

时间:2014-07-26 15:08:11

标签: java

我遇到了问题,我想使用包含电子邮件标题详细信息的txt文件。使用Java我想在该文本中搜索特定的标签,并打印该标签的详细信息。

------------------------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-----------------------

在上面的示例中,有一个名为&#34; subject&#34;的标签。我想打印那个标签及其细节,
例如:Subject:Business Development Meeting,输出中。

4 个答案:

答案 0 :(得分: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();
}

    }

答案 1 :(得分:0)

请找一个非常简单的解决方案..

 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]);
                       }
                    }

                }
            }
    }

输出

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'

答案 2 :(得分:0)

假设你已经编辑了一个你的txt文件:

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'

......你知道它的路径。比,您可以进行简单的while迭代,同时检查当前阅读行是否以特定String值开头。例如,您想要显示一行,其中以“主题:”文本开头。在这种情况下,请查看下面的示例并根据您的需要重新排列。

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();
        }
    }
}

答案 3 :(得分:0)

试试这个。这样您就可以将行Subject: Business Development Meeting存储在字符串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;
}

请注意,如果您指定文件new FileInputStream("file.txt"),那么该文件必须放在项目目录中。否则,您可以为其指定绝对路径。

希望这有帮助