Link to home
Start Free TrialLog in
Avatar of jkrishna01
jkrishna01Flag for United States of America

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

Open in new window

Avatar of contactkarthi
contactkarthi
Flag of United States of America image

should be something like

os = outputFtp.storeFileStream("test.txt");
os.write(XMLtoText.contents.toString().getBytes());
os.close();
ASKER CERTIFIED SOLUTION
Avatar of contactkarthi
contactkarthi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial