• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to best count files in a String

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-1
down vote
favorite


I searched for a long time but didnt find a way to count the number of files in a string, which i get from reading the contents of a rar file (list of filenames and folders) seperated in lines.

Example for the String:



As you can see only item 1,2,3 are files and item 4,5 are folders. I want to count only the number of files (in this case 3).

I suppose i should count the lines in the string which contain a "." before an "\" [the String should be scanned from right to left] and put the value into a int variable? [is there a library or method which can do that?].

[I dont need to adress the exception where a file has no . in it]
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or perhaps does anyone know if there is any other library which can extract the filecount of a rar into an int?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Max Winfield wrote:I suppose i should count the lines in the string which contain a "." before an "\" [the String should be scanned from right to left] and put the value into a int variable?



Not at all. It's possible for folder names to contain a "." and it's possible for file names to not contain a ".". In your example the context tells you that "Folder2" is a folder because there are examples of it being used as a folder, but you can't rely on that happening all the time.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I guess one option would be to use the previous lines (already processed) to determine if the current line (being process) represents a directory.

Henry
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you reading the rar file?
Are you using the zip package in Java?

If so, the ZipEntry class has an isDirectory method...
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you say downvote in your post? Have you been copying from SO or some other site? If so, please tell people (on both websites) about the cross‑posting.
How do you intend to “count” Strings? Maybe the whole text file is one String. Maybe each line is a String. Maybe the \ characters separate the lines into multiple Strings. We would need to know what you consider to be a String to count.
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize. I found the solution by using raroscope library and getting long values for filesizes instead of the string of filenames. I just dont know how to close a thread on coderanch or mark it as solved.
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
didnt know about crossposting please accept my apologies and thank you to Campbell Ritchie for pointing it out (^_^)
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raroscope? Please explain how that works. Did you want to add the sizes of the files?
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Campbell Ritchie!

I opened another thread because this thread name is about best counting things in Strings and not rars. So i thought the best would be to open a second thread which contains the answer to your question as well and can be easier found by anyone searching for the specific problem. (This threadname adresses another problem although i found a better solution which i adress in the other thread)

https://coderanch.com/t/673353/java/filecount-rar-compressed-file
 
reply
    Bookmark Topic Watch Topic
  • New Topic