How to append in a file in Java using OutputStream for different Protocol(FTP,HTTP,SFTP)

355 Views Asked by At

I want to write data to a file(If present then append else create new) present on my ftp server(locally running). I am having a List<byte[]> of a file.

public void write(List<byte[]> bytes, String path)throws Exception{
   URL url = new URL(path);
   URLConnection conn = url.openConnection();
   DataOutputStream out = new DataOutputStream(conn.getOutputStream());
   for (byte[] byte : bytes) {
      out.write(byte);
   }
   out.close();
}

NOTE: Path could be an HTTP or FTP or SFTP url.

It is overwriting on file. I want to append in file which is already present at that location. How to enable append mode in OutputStream?

0

There are 0 best solutions below