Solved

How to write to a text file using FTPClient in Java

Posted on 2008-10-08
2
564 Views
Last Modified: 2013-12-02
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
Comment
Question by:jkrishna01
  • 2
2 Comments
 
LVL 17

Expert Comment

by:contactkarthi
ID: 22672923
should be something like

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

Accepted Solution

by:
contactkarthi earned 500 total points
ID: 22672945
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction People like FTP.  It's a solid, stable, robust protocol for quickly transferring files between two hosts using TCP/IP.  In most cases it's much faster than SMB or CIFS, and certainly much easier to set up between organizations.  This…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now