?
Solved

How to write to a text file using FTPClient in Java

Posted on 2008-10-08
2
Medium Priority
?
583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 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