如何处理文本文件中的多个ArrayList值

huangapple go评论78阅读模式
英文:

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&lt;String&gt; list =  new ArrayList&lt;&gt;();
		list.add(&quot;BBBB&quot;);
		list.add(&quot;CCCC&quot;);
		sftp.writeIntoText(list);
		List&lt;String&gt; list1 =  new ArrayList&lt;&gt;();
		list1.add(&quot;AAAA1111&quot;);
		list1.add(&quot;BBBB2222&quot;);
		list1.add(&quot;CCCC2222&quot;);
		sftp.writeIntoText(list1);
	}
		
	}

 public class SftpConn
      {
    public void writeIntoText(List&lt;String&gt; result) throws Exception
    	{
    		        connect();
                    List&lt;String&gt; resultdata= result;
    				System.out.println(&quot;Result Updated&quot;);
    				channelSftp = (ChannelSftp) session.openChannel(&quot;sftp&quot;);
    				channelSftp.connect();
    				fileOutStream = channelSftp.put(&quot;/home/dasrsoum/RM.txt&quot;);
    				PrintWriter writer = new PrintWriter(fileOutStream,true);
    				writer.println(&quot;------------------&quot;);
    	            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&lt;String&gt; list =  new ArrayList&lt;&gt;();
    list.add(&quot;BBBB&quot;);
    list.add(&quot;CCCC&quot;);
    writeIntoText(list);
    List&lt;String&gt; list1 =  new ArrayList&lt;&gt;();
    list1.add(&quot;AAAA1111&quot;);
    list1.add(&quot;BBBB2222&quot;);
    list1.add(&quot;CCCC2222&quot;);
    writeIntoText(list1);
}
public static void writeIntoText(List&lt;String&gt; result) throws Exception
{
 
    FileWriter fstream = new FileWriter(&quot;log.txt&quot;,true);
    BufferedWriter out = new BufferedWriter(fstream);
    for (String s : result) {
        out.write(s+&quot;\n&quot;);
    }
out.close();
}

huangapple
  • 本文由 发表于 2020年7月24日 16:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63070242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定