Parse the String

Hi,

Below code is Gets a directory list from the server and parses

the elements into a list of files and a list of subdirectories.

I had one problem now is getting a whole string of all those file names and I need to parse it out.

Could anyone know how to change the code below to parse it out the string line by line.

I appreciate it.

Thanks,

chen

private boolean getAndParseDirList(String params, StringBuffer files, StringBuffer dirs)

throws IOException

{

// reset the return variables (we're using StringBuffers instead of

// Strings because you can't change a String value and pass it back

// to the calling routine -- changing a String creates a new object)

files.setLength(0);

dirs.setLength(0);

// get the NLST and the LIST -- don't worry if the commands

// don't work, because we'll just end up sending nothing back

// if that's the case

String shortList = processFileListCommand("nlst " + params);

String longList = processFileListCommand("list " + params);

// tokenize the lists we got, using a newline as a separator

StringTokenizer sList = new StringTokenizer(shortList, "\n");

StringTokenizer lList = new StringTokenizer(longList, "\n");

// other variables we'll need

String sString;

String lString;

// assume that both lists have the same number of elements

while ((sList.hasMoreTokens()) && (lList.hasMoreTokens())) {

sString = sList.nextToken();

lString = lList.nextToken();

if (lString.length() > 0)

{

if (lString.startsWith("d"))

{

dirs.append(sString.trim() + lineTerm);

debugPrint("Dir: " + sString);

} else if (lString.startsWith("-")) {

files.append(sString.trim() + lineTerm);

debugPrint("File: " + sString);

} else {

// actually, symbolic links will start with an "l"

// (lowercase L), but we're not going to mess with

// those

debugPrint("Unknown: " + lString);

}

}

}

// strip off any trailing line terminators and return the values

if (files.length() > 0) { files.setLength(files.length() - lineTerm.length()); }

if (dirs.length() > 0) { dirs.setLength(dirs.length() - lineTerm.length()); }

return true;

}

[2376 byte] By [yuenyuena] at [2007-11-26 13:41:41]
# 1
Sounds to me like a homework... what is the error?MeTitus
Me_Titusa at 2007-7-7 23:58:29 > top of Java-index,Java Essentials,New To Java...
# 2

What exactly is the problem - parsing the String returned by the list commands or parsing the StringBuffer you are modifying?

Please use the [code] tags to make you code readable.

Instead of using StringBuffers, use Collections instead.

And probably better yet, instead of modifying a passed Collection or StringBuffer, return a Collection of your own class which contains both the short and long file name.

jbisha at 2007-7-7 23:58:29 > top of Java-index,Java Essentials,New To Java...
# 3
I don't have an error, but I just get one whole String of all the file name like example below :123.txt444.txt888.txtI want to split with each file name because I am searching the correct file name on the FTP server.Thanks,chen
yuenyuena at 2007-7-7 23:58:29 > top of Java-index,Java Essentials,New To Java...
# 4
What is the value of lineTerm?
jbisha at 2007-7-7 23:58:29 > top of Java-index,Java Essentials,New To Java...
# 5
I declare the lineTerm as below:public String lineTerm = "\n";
yuenyuena at 2007-7-7 23:58:29 > top of Java-index,Java Essentials,New To Java...
# 6

private boolean getAndParseDirList(String params, StringBuffer files, StringBuffer dirs)

throws IOException

{

// reset the return variables (we're using StringBuffers instead of

// Strings because you can't change a String value and pass it back

// to the calling routine -- changing a String creates a new object)

files.setLength(0);

dirs.setLength(0);

// get the NLST and the LIST -- don't worry if the commands

// don't work, because we'll just end up sending nothing back

// if that's the case

String shortList = processFileListCommand("nlst " + params);

String longList = processFileListCommand("list " + params);

// tokenize the lists we got, using a newline as a separator

StringTokenizer sList = new StringTokenizer(shortList, "\n");

StringTokenizer lList = new StringTokenizer(longList, "\n");

// other variables we'll need

String sString;

String lString;

// assume that both lists have the same number of elements

while ((sList.hasMoreTokens()) && (lList.hasMoreTokens())) {

sString = sList.nextToken();

lString = lList.nextToken();

if (lString.length() > 0)

{

if (lString.startsWith("d"))

{

dirs.append(sString.trim() + lineTerm);

debugPrint("Dir: " + sString);

} else if (lString.startsWith("-")) {

files.append(sString.trim() + lineTerm);

debugPrint("File: " + sString);

} else {

// actually, symbolic links will start with an "l"

// (lowercase L), but we're not going to mess with

// those

debugPrint("Unknown: " + lString);

}

}

}

// strip off any trailing line terminators and return the values

if (files.length() > 0) { files.setLength(files.length() - lineTerm.length()); }

if (dirs.length() > 0) { dirs.setLength(dirs.length() - lineTerm.length()); }

return true;

}

yuenyuena at 2007-7-7 23:58:29 > top of Java-index,Java Essentials,New To Java...
# 7

> I declare the lineTerm as below:

>

> public String lineTerm = "\n";

How are you determining that the value in the end is:

123.txt444.txt888.txt

Could the new line be being swallowed by how you are displaying the value?

Also, if you are FTPing the output, I believe that FTP could effect the new line in some cases (cannot remember when, how).

Try using a simple char (~. |, *, etc.) and see if they show up.

jbisha at 2007-7-7 23:58:29 > top of Java-index,Java Essentials,New To Java...