readline() and process !

please see the code below :

publicstaticvoid main(String argsp[])throws IOException,InterruptedException{

Runtime r=Runtime.getRuntime();

Process p=null;

try{

p=r.exec("lftp xxx.xxx.xxx.xxx");

}catch(Exception e){

System.out.println("Error executing lftp");

}

PrintWriter out1 =new PrintWriter(new OutputStreamWriter(p.getOutputStream()));

BufferedReader in1 =new BufferedReader(new InputStreamReader(p.getInputStream()));

String buff="";

String cmd="ls";

out1.println(cmd);

out1.flush();

while((buff=in1.readLine())!=null)// the while will loop forever !

{

System.out.println(buff);

}

}

the code output the correct information ,then the readline block and the while won't jump out.how can I solve it ?

please help !

[1627 byte] By [jbbskill] at [2007-9-30 20:06:12]
# 1

FTP doesn't close the socket, so there is no end of file, so readLine() doesn't return null.

"ls"'s output seems to be ended by a line like "226 10 matches total". "226" is the response status code. Add to the while loop:

if (buff.startsWith("226 ")) // There is a space after the status code

break;

I wonder what happens if the directory contains a file named "226 bottles of beer on the wall.txt". You may want to experiment.

If you want to write a more robust ftp thingie, check out what other response codes are possible, and deal with each one. Google for the FTP RFC.

sjasja at 2007-7-7 0:52:17 > top of Java-index,Java Essentials,Java Programming...
# 2
Oh, and if you need to make a real production quality ftp interface, there are libraries around (of varying quality...) Google for java ftp[/url].
sjasja at 2007-7-7 0:52:17 > top of Java-index,Java Essentials,Java Programming...
# 3

I added it ,but it doesn't work.

by the way , "ls" 's output isn't ended by a line like "226 ..".

the output is just some files and directories !

all I want to know is how to get the output if the application doesn't close !

or use another method to obtain the output of the inputStream of the process ?

jbbskill at 2007-7-7 0:52:17 > top of Java-index,Java Essentials,Java Programming...
# 4
Duplicate see http://forum.java.sun.com/thread.jsp?forum=31&thread=563570&tstart=0&trange=100
ChuckBing at 2007-7-7 0:52:17 > top of Java-index,Java Essentials,Java Programming...