jkrishna01
asked on
How to write to a text file using FTPClient in Java
I am trying to convert an XML file into a text file in the server. I am able to read the xml file using SAX parser, I am storing it in CharArrayWriter. But I am having problem writing this character array to a text file. I don't know which output stream should be used.
// Buffer for collecting data from the "characters" SAX event.
private CharArrayWriter contents = new CharArrayWriter();
// SAX events go here... The characters are stored in contents.
inputFtp = new FTPClient();
outputFtp = new FTPClient();
inputFtp.connect("aaa");
outputFtp.connect("aaa");
inConn = inputFtp.login("bbb", "ccc");
outConn = outputFtp.login("bbb", "ccc");
// Create SAX 2 parser...
XMLReader xr = XMLReaderFactory.createXMLReader();
// Set the ContentHandler...
ProcessXMLtoTextinServer XMLtoText = new ProcessXMLtoTextinServer();
xr.setContentHandler( XMLtoText );
inputFtp.setFileType(FTPClient.BINARY_FILE_TYPE);
is = inputFtp.retrieveFileStream("test.xml");
int reply=inputFtp.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
//LOG.info("Connected Successfully to the FTP server");
}else{
//LOG.info("Connection to the FTP server Failed");
inputFtp.disconnect();
//System.exit(1);
}
// parse the file...
xr.parse(new InputSource(new InputStreamReader(is )) );
os = outputFtp.storeFileStream("test.txt");
OutputStreamWriter osw = new OutputStreamWriter(os,XMLtoText.contents.toString()); //The problem is in this line.
BufferedWriter out = new BufferedWriter(osw);
out.close();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
os = outputFtp.storeFileStream("test.txt");
os.write(XMLtoText.content
os.close();