Solved

How to write to a text file using FTPClient in Java

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

Do email signature updates give you a headache?

Are you constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 80
eclipse shortcuts 9 45
ForLoop Example 3 40
Windows 10 IE Certificate Issue 10 41
Goal:  To set up a secure SSH server for your home computer to make it accessible anywhere AND to use it as a port forwarding proxy. Steps 1.  WinSSHD version 5 is free for personal use.  So download and install it.  You can download it from the…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

911 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

15 Experts available now in Live!

Get 1:1 Help Now