Solved

How to write to a text file using FTPClient in Java

Posted on 2008-10-08
2
569 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

17 Experts available now in Live!

Get 1:1 Help Now