Java - 从文本文件中获取 ArrayList 内容并输出到单独的行 (JavaMail API)
Java - Get ArrayList contents from a text file and output to separate lines (JavaMail API)
我正在尝试读取一个包含多行文本的文本文件。现在我想将它输出到控制台,一次一行,因为这是文本文件的格式。很简单,对吧?我们可以用一个简单的 for 循环来做到这一点,像这样:
lineList.forEach(System.out::println),
现在的问题是,我实际上使用的是 JavaMail API,特别是访问只接受 String 类型的 message.setText 方法。
我想知道如何在 setText 方法中将 ArrayList 输出到分隔行。我在想也许可以将该 for 循环存储为一个变量并在方法中调用它?
ArrayList<String> lineList = new ArrayList<>();
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(
file));
// Add all lines from file to ArrayList.
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
lineList.add(line);
}
// Close it.
reader.close();
// Print each line.
for (String line : lineList) {
System.out.println(line);
}
// SetText method from the JavaMail API that does not accept the for-loop
message.setText(lineList.forEach(System.out::println);
如果我查看此 JavaDoc (https://javamail.java.net/nonav/docs/api/javax/mail/Part.html),我认为这应该可行:
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(file));
// Add all lines from file to ArrayList.
while (true){
String line = reader.readLine();
if(line==null){
break;
}
lineList.add(line);
}
// Close it.
reader.close();
StringBuilder sb = new StringBuilder();
// Print each line.
for (String line : lineList){
sb.append(line);
}
// SetText method from the JavaMail API that does not accept the for-loop
message.setText(sb.toString());
如评论中所建议,您可以使用流将字符串列表转换为字符串。
message.setText(lineLisit.stream().collect(Collectors.joining("\n"))
否则,由于 BufferedReader#ReadLine 删除了 Line Feeds,您可以避免使用 BufferedReader。这是其他选项之一。
try {
final byte[] data = Files.readAllBytes(Paths.get("<your file>"));
message.setText(new String(data);
} catch (IOException e) {
e.printStackTrace();
}
注意:为了将字节正确转换为字符串,最好使用正确的字符集。
我正在尝试读取一个包含多行文本的文本文件。现在我想将它输出到控制台,一次一行,因为这是文本文件的格式。很简单,对吧?我们可以用一个简单的 for 循环来做到这一点,像这样:
lineList.forEach(System.out::println),
现在的问题是,我实际上使用的是 JavaMail API,特别是访问只接受 String 类型的 message.setText 方法。
我想知道如何在 setText 方法中将 ArrayList 输出到分隔行。我在想也许可以将该 for 循环存储为一个变量并在方法中调用它?
ArrayList<String> lineList = new ArrayList<>();
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(
file));
// Add all lines from file to ArrayList.
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
lineList.add(line);
}
// Close it.
reader.close();
// Print each line.
for (String line : lineList) {
System.out.println(line);
}
// SetText method from the JavaMail API that does not accept the for-loop
message.setText(lineList.forEach(System.out::println);
如果我查看此 JavaDoc (https://javamail.java.net/nonav/docs/api/javax/mail/Part.html),我认为这应该可行:
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(file));
// Add all lines from file to ArrayList.
while (true){
String line = reader.readLine();
if(line==null){
break;
}
lineList.add(line);
}
// Close it.
reader.close();
StringBuilder sb = new StringBuilder();
// Print each line.
for (String line : lineList){
sb.append(line);
}
// SetText method from the JavaMail API that does not accept the for-loop
message.setText(sb.toString());
如评论中所建议,您可以使用流将字符串列表转换为字符串。
message.setText(lineLisit.stream().collect(Collectors.joining("\n"))
否则,由于 BufferedReader#ReadLine 删除了 Line Feeds,您可以避免使用 BufferedReader。这是其他选项之一。
try {
final byte[] data = Files.readAllBytes(Paths.get("<your file>"));
message.setText(new String(data);
} catch (IOException e) {
e.printStackTrace();
}
注意:为了将字节正确转换为字符串,最好使用正确的字符集。