Java-从文本文件获取ArrayList内容并输出到单独的行(JavaMail API)
我正在尝试读取包含多行文本的文本文件。现在我想把它输出到控制台,一次输出一行,因为这是文本文件的格式。很简单,对吧?我们可以通过一个简单的for循环来实现这一点,如下所示:Java-从文本文件获取ArrayList内容并输出到单独的行(JavaMail API),java,list,for-loop,arraylist,jakarta-mail,Java,List,For Loop,Arraylist,Jakarta Mail,我正在尝试读取包含多行文本的文本文件。现在我想把它输出到控制台,一次输出一行,因为这是文本文件的格式。很简单,对吧?我们可以通过一个简单的for循环来实现这一点,如下所示: lineList.forEach(System.out::println), 现在的问题是,我实际上在使用JavaMail API,特别是访问message.setText方法,该方法只接受字符串类型 我想知道如何在setText方法中将ArrayList输出到单独的行。我在想,也许可以将for循环存储为变量并在方法中调用
lineList.forEach(System.out::println),
现在的问题是,我实际上在使用JavaMail API,特别是访问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);
ArrayList lineList=new ArrayList();
//新的BufferedReader。
BufferedReader reader=新BufferedReader(新文件读取器(
档案);;
//将文件中的所有行添加到ArrayList。
while(true){
字符串行=reader.readLine();
如果(行==null){
打破
}
lineList.add(行);
}
//关上它。
reader.close();
//打印每一行。
用于(字符串行:行列表){
系统输出打印项次(行);
}
//JavaMail API中不接受for循环的SetText方法
message.setText(lineList.forEach(System.out::println);
如果我看一下这个JavaDoc(),我认为这应该是可行的:
// 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());
如果我看一下JavaDoc(),我认为这应该是可行的:
// 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删除了换行符,因此可以避免使用BufferedReader。以下是其他选项之一
try {
final byte[] data = Files.readAllBytes(Paths.get("<your file>"));
message.setText(new String(data);
} catch (IOException e) {
e.printStackTrace();
}
试试看{
final byte[]data=Files.readAllBytes(path.get(“”));
message.setText(新字符串(数据);
}捕获(IOE异常){
e、 printStackTrace();
}
注意:要正确地将字节转换为字符串,最好使用正确的字符集。如注释中所建议的,您可以使用流将字符串列表转换为字符串
message.setText(lineLisit.stream().collect(Collectors.joining("\n"))
或者,由于BufferedReader#ReadLine删除了换行符,因此可以避免使用BufferedReader。以下是其他选项之一
try {
final byte[] data = Files.readAllBytes(Paths.get("<your file>"));
message.setText(new String(data);
} catch (IOException e) {
e.printStackTrace();
}
试试看{
final byte[]data=Files.readAllBytes(path.get(“”));
message.setText(新字符串(数据);
}捕获(IOE异常){
e、 printStackTrace();
}
注意:要正确地将字节转换为字符串,最好使用正确的字符集。使用行分隔符.message.setText(lineLisit.stream().collect(Collectors.joining(“\n”))转换为字符串谢谢,成功了!使用行分隔符.message.setText(lineLisit.stream().collector(Collectors.joining(“\n”))转换为字符串谢谢,成功了!如果您想在字里行间更改某人附加(行);更改某人附加(行+“\n”);如果您想在字里行间更改某人附加(行);更改某人附加(行+“\n”);太好了,谢谢!太好了,谢谢!