• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 603
  • Last Modified:

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

0
jkrishna01
Asked:
jkrishna01
  • 2
1 Solution
 
contactkarthiCommented:
should be something like

os = outputFtp.storeFileStream("test.txt");
os.write(XMLtoText.contents.toString().getBytes());
os.close();
0
 
contactkarthiCommented:
ftp:// got appeneded in the previous post for some reason....

below is the one i wanted you to try

os = outputFtp.storeFileStream("test.txt");
os.write(XMLtoText.contents.toString().getBytes());
os.close();
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now