简体   繁体   中英

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?

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?

Have you tried JavaMail ? You included that tag so I assume you know what it is.

Use the MimeMessage constructor that takes an InputStream . Note that the file actually has to be in MIME format, which your above example is not exactly.

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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