如何在Java Spring Boot中读取文件路径中的文本



我正在使用Java Spring Boot构建一个简单的桌面应用程序,在下面EmailSenderHandler类的构造函数中,我想将this.emailBody属性设置为存储在htmlFilePath中的HTML文件中的文本内容。我想不出合适的方法来做这件事,有人能帮我吗?提前谢谢。

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

也许你可以稍后阅读正文,


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

并实现了readEmailBodyFromHtml方法

我认为您可能希望使用字符输入流来读取html文件并添加到邮件正文字符串中。因此,首先创建new File(htmlFilePath),然后在构造函数中使用文件应用一个新的字符流实例。

最新更新