-2

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();
   }

Actual output

 AAAA1111
   BBBB2222
   CCCC2222

Excepted OutPut

  AAAA
  BBBB
  CCCC
  AAAA1111
  BBBB2222
  CCCC2222
  • 1
    You always create a new field! use the constructor with append parameter https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String,%20boolean) – Jens Jul 23 '20 at 07:08
  • Does this answer your question? [how to write an array to a file Java](https://stackoverflow.com/questions/13707223/how-to-write-an-array-to-a-file-java) – fuggerjaki61 Jul 23 '20 at 08:35

1 Answers1

3

You first file is overwritten by the second call to the function

Pierre Sevrain
  • 699
  • 4
  • 14
  • Yes, he just need to use `FileWriter writer = new FileWriter("C:\\Users\\Documents\\Result\\Test.txt", true);` as stated by @Jens – Rohan Sharma Jul 23 '20 at 07:16
  • @RohanSharma Thank You So much,can you suggest one more thing , i want when every time when i will execute my code it should remove my previous data from text file and write current data – Nibedita Behera Jul 23 '20 at 07:31
  • @NibeditaBehera Do not remove data from file, just replace file, overwrite new file with same name in same place – Basil Bourque Jul 23 '20 at 07:39
  • You can delete the file at the start of your main function – Pierre Sevrain Jul 23 '20 at 07:52
  • @RohanSharma I have updated my code now i'm trying to write a file in the sftp location, but still my expected out put is not coming – Nibedita Behera Jul 24 '20 at 08:15