简体   繁体   English

如何从Java中的txt文件提取以下信息

[英]How can I extract the following information from a txt file in java

For example I'm writing an email (console) based application for fun. 例如,我正在编写一个基于电子邮件(控制台)的应用程序以取乐。 I was trying to incorporate files into it to read the information from it. 我试图将文件合并到其中以从中读取信息。 For example if my txt format is as following how can I read each variable? 例如,如果我的txt格式如下,如何读取每个变量?

Server: gmail
User: test@mail.com
Password: pass123
To: to@mail.com
CC: to@mail.com, to@mail.com, to@mail.com
BCC: to@mail.com, to@mail.com
Subject: subject
Body: 123
454
6464
This is still part of the body
File: filename.zip

However, the CC and BCC should be a string array I believe, right? 但是,我相信CC和BCC应该是一个字符串数组,对吗?

Have you tried JavaMail ? 您尝试过JavaMail吗? You included that tag so I assume you know what it is. 您包括了该标签,所以我假设您知道它是什么。

Use the MimeMessage constructor that takes an InputStream . 使用采用InputStreamMimeMessage构造函数 Note that the file actually has to be in MIME format, which your above example is not exactly. 请注意,该文件实际上必须是MIME格式,您上面的示例并不完全是。

As Bill Shannon says above, your question is a bit vague, but I gather that you're trying to find a way to read key/value pairs from a text file so that you can use these to programmatically compose an email. 正如Bill Shannon所说,您的问题有点含糊,但我认为您正在尝试找到一种从文本文件中读取键/值对的方法,以便您可以使用这些方法以编程方式编写电子邮件。 If so, I believe you would want to proceed by doing something like as follows. 如果是这样,我相信您将希望通过执行以下操作来继续。

First, the data you listed above are essentially name/value pairs delimited as: 首先,您上面列出的数据实际上是名称/值对,分隔为:

name1: value1
name2: value2
...
nameN: valueN

If so, the correct convention for doing this would be to use a .properties file. 如果是这样,正确的约定是使用.properties文件。 In order to do that, you want your key/value pairs delimited instead as: 为此,您希望将键/值对定界为:

name1=value1
name2=value2
...
nameN=valueN

Hence, the data you have above would be something like: 因此,您上面的数据将类似于:

server=gmail
user=test@mail.com
password=pass123
to=to@mail.com
cc=cc1@mail.com,cc2@mail.com,cc3@mail.com
bcc=to@mail.com,to@mail.com
subject=This is my Subject
body=123 \
454 \
6464 \
This is still part of the body
file.name=filename.zip

Notice the backslashes at the end of each line of the "body" property. 请注意,在“ body”属性的每一行末尾都使用反斜杠。 These escape the line break so that you end up with just one line. 它们避开了换行符,因此您最终只能获得一行。

If you save the above into a file called email.properties, you can access these in a Java program like so: 如果将以上内容保存到名为email.properties的文件中,则可以像这样在Java程序中访问它们:

import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

public class EmailComposer {

    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();
        properties.load(new FileReader("email.properties"));

        for (Map.Entry<Object, Object> property : properties.entrySet()) {
            String key = property.getKey().toString();
            String value = property.getValue().toString();
            System.out.printf("%s --> [%s]\n", key, value);
        }
    }
}

Which would produce the following output: 这将产生以下输出:

cc --> [cc1@mail.com,cc2@mail.com,cc3@mail.com]
server --> [gmail]
user --> [test@mail.com]
body --> [123 454 6464 This is still part of the body]
bcc --> [to@mail.com,to@mail.com]
subject --> [This is my Subject]
to --> [to@mail.com]
password --> [pass123]
file.name --> [filename.zip]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM