简体   繁体   中英

How can I read text in a file path in Java Spring Boot?

I'm using Java Spring Boot to build a simple desktop app and there, in the following constructor for the class EmailSenderHandler I want to set this.emailBody property to the text content in the HTML file stored in htmlFilePath . I can't think of a proper method to do that, can anyone help me with that? Thanks in advance.

public EmailSenderHandler(String inputFilePath, 
                          String csvFilePath, 
                          String htmlFilePath, 
                          String fromEmail, 
                          String password, 
                          String fromEmailName, 
                          String emailBody, 
                          AtomicLong progressCount, 
                          DataProcessor dataProcessor)
{

        this.inputFilePath = inputFilePath;
        this.csvFilePath = csvFilePath;
        this.htmlFilePath = htmlFilePath;
        this.fromEmail = fromEmail;
        this.password = password;
        this.fromEmailName = fromEmailName;
        this.emailBody = emailBody;
        this.progressCount = progressCount;
        this.dataProcessor = dataProcessor;
    }

I think you might want to utilize a character input stream to read the html file and add to the mail body string.So first of all you create new File(htmlFilePath) then you apply a new instance of character stream with the file in the constructor.

Maybe you can read the body later,


public EmailSenderHandler(String inputFilePath, 
                          String csvFilePath, 
                          String htmlFilePath, 
                          String fromEmail, 
                          String password, 
                          String fromEmailName, 
                          AtomicLong progressCount, 
                          DataProcessor dataProcessor)
{
        this.inputFilePath = inputFilePath;
        this.csvFilePath = csvFilePath;
        this.htmlFilePath = htmlFilePath;
        this.fromEmail = fromEmail;
        this.password = password;
        this.fromEmailName = fromEmailName;
        this.progressCount = progressCount;
        this.dataProcessor = dataProcessor;

        this.emailBody = readEmailBodyFromHtml(htmlFilePath);
    }

and implement the readEmailBodyFromHtml method

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