如何在文本文件中写入多个ArrayList [英] How to write multiple ArrayList in a text file
本文介绍了如何在文本文件中写入多个ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,我正在尝试创建一个txt文件,并且我想在其中添加一些值列表. 因此,在这里,我的方法是在文件不存在的情况下创建一个新文件,并向其中添加各个元素.
In below code , i'm trying to create a txt file and there i want to add some list of values. So here my approach is create a new file if file is not present and add the respective elements into it.
请参阅下面的内容,我无法获得预期的输出,因此,您能为我提供一些帮助吗,这将非常有帮助
Please see below , i'm unable to get my expected output, So can you help me with some idea so it will be very helpful
public class MyTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
SftpConn sftp = new SftpConn();
//sftp.sftpGetConnect();
List<String> list = new ArrayList<>();
list.add("AAAA");
list.add("BBBB");
list.add("CCCC");
sftp.writeIntoText(list);
List<String> list1 = new ArrayList<>();
list1.add("AAAA1111");
list1.add("BBBB2222");
list1.add("CCCC2222");
sftp.writeIntoText(list1);
}
}
public class SftpConn
{
public void writeIntoText(List<String> result) throws Exception
{
connect();
List<String> resultdata= result;
System.out.println("Result Updated");
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
fileOutStream = channelSftp.put("/home/dasrsoum/RM.txt");
PrintWriter writer = new PrintWriter(fileOutStream,true);
writer.println("------------------");
for (String value : resultdata) {
writer.println(value+ System.lineSeparator());
System.out.println(value);
}
writer.close();
}
实际输出
AAAA1111
BBBB2222
CCCC2222
OutPut除外
AAAA
BBBB
CCCC
AAAA1111
BBBB2222
CCCC2222
推荐答案
您的第一个文件被第二次调用该函数覆盖
You first file is overwritten by the second call to the function
这篇关于如何在文本文件中写入多个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文