質問

I'm writing FTP client handler in Java and I can't use FTP libraries like Apache.

My problem is that I receive the list from the server in this format:

drw-rw-rw- 1 ftp ftp                0 Mar 17 06:10 Tor Browser
-rw-rw-rw- 1 ftp ftp          1538814 Jun 26 00:23 setup.exe
-rw-rw-rw- 1 ftp ftp           142570 May 24 05:28 satellite A665-S6086.pdf

While all I need is the file/directory name and size.

Please suggest me a way to reduce the list to names and sizes, keeping in mind spacing differences between the columns and spacing in the filenames.

Thank you all in advance :)

役に立ちましたか?

解決

Since you just want to grab the data, you can use this regex:

"(?m)^.{20}\\s*(\\d+).{14}(.*)$"

And construct a Pattern, and obtain a Matcher corresponding to the input string and start extracting matches. The size can be obtained in group(1) and the file name can be obtained in group(2).

他のヒント

Use

    Pattern sizeAndNamePattern = Pattern.compile(
            "^-.*?(\\d+) \\w{3} \\d{2} \\d{2}:\\d{2} (.*)$", Pattern.MULTILINE);

    for (Matcher matcher = sizeAndNamePattern.matcher(dirListing); matcher.find();) {
        System.out.println(matcher.group(1) + " " + matcher.group(2));
    }

If you also want to see directories, than remove the first "-" in the pattern. If performance is an issue, you should consider a more selective pattern that avoids the .*? at the beginning.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top