I think a lot depends on what your final goal is. If it is just to print out the data, or if you need to process the data, or if you need to manipulate it algorithmically.
My gut instinct would be to do it all using LinkedHashMap and basically make it a map of a map of a map, etc... It can get a little tricky though to do this cleanly since you have two different possible values for your map - folder and file. Note that the use of the LinkedHashMap is to maintain the order of the data as you insert it. If you want it sorted, or random order, then you can choose other implementations.
Essentially, I would see it as something like the following (pseudo-code)
public interface DirectoryEntry{
boolean isFile;
boolean isFolder;
}
public class Folder implements DirectoryEntry{
public boolean isFile(){ return false;}
public boolean isFolder(){ return true;}
private Map<String, DirectoryEntry> entries = new LinkedHashMap<String, DriectoyrEntry>();
public Map<String, DirectoryEntry>getEntries(){ return entries;}
public void addFile( String filename ){ entries.put( filename, new File();}
public void addFolder( String foldername){entries.put(foldername, new Folder();}
}
public class File implements DirectoryEntry{
public boolean isFile(){ return true;}
public boolean isFolder(){ return false;}
}
Then build the tree (you can do this recursively as well):
Map<String, DirectoryEntry> entries = new LinkedHashMap<String, DirectoryEntry>()
while( !end of list){
Map entry = entries;
1. Split string on '/'
2. foreach( token ){
if file
entries.put( token, new File() );
else{
if( !entry.containsKey( token ) )
entry.put(token, new Folder );
// use the folder map for any sub-folders/files
entry = ((Folder)entry.get( token )).getEntries();
}
}
}
Then you can scan through your final list and play with it as you like. I've created File and Folder intentionally basic, but you can add additional metadata/info there if you have/need.
Keep in mind that this is just an option to give you an idea as to how to parse/map the files/folders, but not meant as a functional solution as is.
As for if it is easier to parse first and print, like I said, it all depends what you hope to achieve with a final data set. But my gut instinct would be to parse it all and then display what you have parsed to ensure that what you are showing is what you have "understood" and will be manipulating.