英文:
How to handle multiple arraylist value in a text file
问题
在下面的代码中,我试图创建一个文本文件,并在其中添加一些值的列表。我的方法是:如果文件不存在,则创建一个新文件,并将相应的元素添加到其中。
请看下面,我无法获得预期的输出,所以你能帮我提供一些想法吗?这将非常有帮助。
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("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();
}
}
实际输出
BBBB2222
CCCC2222
期望输出
BBBB
CCCC
AAAA1111
BBBB2222
CCCC2222
英文:
In below code, I'm trying to create a text file and there I want to add some list of values. My approach is to 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("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();
}
Actual output
BBBB2222
CCCC2222
Expected output
BBBB
CCCC
AAAA1111
BBBB2222
CCCC2222
答案1
得分: 0
你可以简单地使用一个类和两个方法,如下所示:
只需使用for-each循环和FileWriter类来向文件中添加内容。
public static void main(String[] args) throws Exception {
//sftp.sftpGetConnect();
List<String> list = new ArrayList<>();
list.add("BBBB");
list.add("CCCC");
writeIntoText(list);
List<String> list1 = new ArrayList<>();
list1.add("AAAA1111");
list1.add("BBBB2222");
list1.add("CCCC2222");
writeIntoText(list1);
}
public static void writeIntoText(List<String> result) throws Exception {
FileWriter fstream = new FileWriter("log.txt", true);
BufferedWriter out = new BufferedWriter(fstream);
for (String s : result) {
out.write(s + "\n");
}
out.close();
}
英文:
You can simply use one class and 2 methods as below:
Simply add content in the File using for-each loop and FileWriter class.
public static void main(String[] args) throws Exception {
//sftp.sftpGetConnect();
List<String> list = new ArrayList<>();
list.add("BBBB");
list.add("CCCC");
writeIntoText(list);
List<String> list1 = new ArrayList<>();
list1.add("AAAA1111");
list1.add("BBBB2222");
list1.add("CCCC2222");
writeIntoText(list1);
}
public static void writeIntoText(List<String> result) throws Exception
{
FileWriter fstream = new FileWriter("log.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
for (String s : result) {
out.write(s+"\n");
}
out.close();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论