Suggest how to write a text file which is available in SFTP location
1.Class A is my main exactly i want write all my result in .txt which is available in sftp location.
2.In class B i have created .txt file in the sftp location in that same text file i want to put my print values.
3.Below code it testNg code
@Test public class A { B b = new B(); int i=10; int j = 20; int k=i+j; System.out.println(k); } } public class B { public String sftrWriteFile() { try { connect(); channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); System.out.println("Session connected " + session.isConnected()); //fileOutStream = channelSftp.put("/home/dasrsoum/RM_Test_Result.xlsx"); fileOutStream = channelSftp.put("/home/dasrsoum/RM_Test_Result.xlsx"); // wb = new XSSFWorkbook(fileStream); // sheet = wb.getSheetAt(0); fileOutStream.close(); session.disconnect(); } catch (Exception e) { e.printStackTrace(); } return "Sucess"; } }
You can use PrintWriter
decorator to OutputStream
to simplify writing to a text file. Let’s assume that you are passing int[] data
to sftrWriteFile()
method of class B i.e. the signature changes as sftrWriteFile(int data)
. Following code snippet will help you to understand the idea:
OutputStream fileOutStream = channelSftp.put("/home/dasrsoum/RM_Test_Result.txt"); PrintWriter printWriter = new PrintWriter(fileOutStream, true); for (int value : data) { printWriter.println(value); } printWriter.close();