mimic directory structure , array of objects

Discussion in 'Java' started by michael, Sep 5, 2004.

  1. michael

    michael Guest

    Hi,

    I need to write a program that mimics directory structure on PC or UNIX.
    after loading the directory structure into such an object , I should be able to
    print a particulr node :

    for axample if filea.txt has a path of c:\test\mydir\michael\myfiles\filea.txt
    then the assignment should be :

    dir[0].dir[1].dir[2].dir[3].filename[0]=
    "c:\test\mydir\michael\myfiles\filea.txt";

    any suggesions is greatly appreciated.

    thanks
    Michael
     
    michael, Sep 5, 2004
    #1
    1. Advertising

  2. Andrew Thompson, Sep 5, 2004
    #2
    1. Advertising

  3. michael

    Sudsy Guest

    michael wrote:
    > Hi,
    >
    > I need to write a program that mimics directory structure on PC or UNIX.
    > after loading the directory structure into such an object , I should be able to
    > print a particulr node :
    >
    > for axample if filea.txt has a path of c:\test\mydir\michael\myfiles\filea.txt
    > then the assignment should be :
    >
    > dir[0].dir[1].dir[2].dir[3].filename[0]=
    > "c:\test\mydir\michael\myfiles\filea.txt";


    See java.io.File#getParentFile. Work your way up the tree and store the
    elements in something which supports insert at a specified position.
    Here's some code to get you started:

    private static final String path = "/usr/local/bin/ksh";

    public static void main( String args[] ) {
    File f = new File( path );
    ArrayList l = new ArrayList();
    while( f.getParentFile() != null ) {
    try {
    l.add( 0, f.getName() );
    }
    catch( Exception e ) {
    e.printStackTrace();
    System.exit( 4 );
    }
    f = f.getParentFile();
    }
    for( int i = 0; i < l.size(); i++ )
    System.out.println( l.get( i ) );
    }

    End conditions and error checking (null path, etc.) left as an
    exercise for the reader.
     
    Sudsy, Sep 5, 2004
    #3
  4. michael

    Paul Lutus Guest

    michael wrote:

    > Hi,
    >
    > I need to write a program that mimics directory structure on PC or UNIX.
    > after loading the directory structure into such an object , I should be
    > able to print a particulr node :
    >
    > for axample if filea.txt has a path of
    > c:\test\mydir\michael\myfiles\filea.txt then the assignment should be :
    >
    > dir[0].dir[1].dir[2].dir[3].filename[0]=
    > "c:\test\mydir\michael\myfiles\filea.txt";
    >
    > any suggesions is greatly appreciated.


    Here's my suggestion. Since this is obviously homework, post your code ands
    someone will help you get it working.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 5, 2004
    #4
  5. michael

    Paul Lutus Guest

    Sudsy wrote:

    > michael wrote:
    >> Hi,
    >>
    >> I need to write a program that mimics directory structure on PC or UNIX.
    >> after loading the directory structure into such an object , I should be
    >> able to print a particulr node :
    >>
    >> for axample if filea.txt has a path of
    >> c:\test\mydir\michael\myfiles\filea.txt then the assignment should be :
    >>
    >> dir[0].dir[1].dir[2].dir[3].filename[0]=
    >> "c:\test\mydir\michael\myfiles\filea.txt";

    >
    > See java.io.File#getParentFile.


    Did you even read his post? Okay, then, did you understand it?

    > Work your way up the tree and store the
    > elements in something which supports insert at a specified position.
    > Here's some code to get you started:
    >
    > private static final String path = "/usr/local/bin/ksh";
    >
    > public static void main( String args[] ) {
    > File f = new File( path );
    > ArrayList l = new ArrayList();
    > while( f.getParentFile() != null ) {
    > try {
    > l.add( 0, f.getName() );
    > }
    > catch( Exception e ) {
    > e.printStackTrace();
    > System.exit( 4 );
    > }
    > f = f.getParentFile();
    > }
    > for( int i = 0; i < l.size(); i++ )
    > System.out.println( l.get( i ) );
    > }
    >
    > End conditions and error checking (null path, etc.) left as an
    > exercise for the reader.


    This is a perfect code post for someone who needs to do his own homework. It
    doesn't address what he wants, it doesn't mimic the underlying filesystem,
    and it doesn't get him started in solving the original problem.

    One might almost imagine that this was the intention.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 5, 2004
    #5
  6. michael

    Sudsy Guest

    Paul Lutus wrote:
    > Sudsy wrote:
    >>michael wrote:

    <snip>
    >>>dir[0].dir[1].dir[2].dir[3].filename[0]=
    >>>"c:\test\mydir\michael\myfiles\filea.txt";

    >>
    >>See java.io.File#getParentFile.

    >
    >
    > Did you even read his post? Okay, then, did you understand it?


    I admit to not understanding much, but it appeared that the OP
    was looking to separate the directory tree into consituent
    elements, stored in an array structure.
    Was this not your interpretation?
    I just figured that using the methods in java.io.File would be
    the most expeditious route.
    YMMV
     
    Sudsy, Sep 5, 2004
    #6
  7. michael

    Paul Lutus Guest

    Sudsy wrote:

    > Paul Lutus wrote:
    >> Sudsy wrote:
    >>>michael wrote:

    > <snip>
    >>>>dir[0].dir[1].dir[2].dir[3].filename[0]=
    >>>>"c:\test\mydir\michael\myfiles\filea.txt";
    >>>
    >>>See java.io.File#getParentFile.

    >>
    >>
    >> Did you even read his post? Okay, then, did you understand it?

    >
    > I admit to not understanding much, but it appeared that the OP
    > was looking to separate the directory tree into consituent
    > elements, stored in an array structure.


    But your code doesn't do this. It climbs a directory tree, from a specific
    node to the root. He wants a traversal in depth, so the entire
    file/directly tree is mirrored in a tree of collection classes in his
    program, so he can specify any node using a series of index numbers. His
    post shows this unambiguously -- he shows the use of index numbers to refer
    to successive elements in a large tree, each node of which may have any
    number of branches.

    In other words, the entire filesystem directory tree, from a chosen
    beginning point, is duplicated in his program, and any node in that tree
    can be accessed using a set of index numbers.

    > Was this not your interpretation?


    The difference is I realized he shouldn't be given the result before he
    posts his own code.

    > I just figured that using the methods in java.io.File would be
    > the most expeditious route.


    Yes, but your code doesn't traverse the tree in depth, instead it climbs it,
    once.

    In summary, and again, your post was perfect. If the OP expects easy
    answers, he got one. I just hope he doesn't simply turn it in without
    asking himself whether it addresses the original assignment.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 5, 2004
    #7
  8. michael

    michael Guest

    Paul Lutus <> wrote in message news:<>...
    > michael wrote:
    >
    > > Hi,
    > >
    > > I need to write a program that mimics directory structure on PC or UNIX.
    > > after loading the directory structure into such an object , I should be
    > > able to print a particulr node :
    > >
    > > for axample if filea.txt has a path of
    > > c:\test\mydir\michael\myfiles\filea.txt then the assignment should be :
    > >
    > > dir[0].dir[1].dir[2].dir[3].filename[0]=
    > > "c:\test\mydir\michael\myfiles\filea.txt";
    > >
    > > any suggesions is greatly appreciated.

    >
    > Here's my suggestion. Since this is obviously homework, post your code ands
    > someone will help you get it working.


    Hi,
    No this is not homework . let me make this a bit more specefic.
    my project is very simliar to a two level directory structures with
    multiple files at each level.
    each directory has properties ( size , date , security ) and each file
    has a similar properties. therefore c:\ is the root and
    c:\a c:\b , c:\c are directories and each directory has 1 to n files.
    the idea is to fully describe this object and be able to traverse this
    simple
    tree.

    thanks
    Michael
     
    michael, Sep 5, 2004
    #8
  9. michael

    Paul Lutus Guest

    michael wrote:

    > Paul Lutus <> wrote in message


    / ...

    >> Here's my suggestion. Since this is obviously homework, post your code
    >> ands someone will help you get it working.

    >
    > Hi,
    > No this is not homework .


    Don't bother saying this. You don't know how to traverse a tree, programmers
    know how to traverse trees, therefore you are learning how to program. But
    paradoxically you are not a student. Conclusion? Don't bother saying you
    are not a student, and this is not homework. It's just a waste of time.
    It's like saying, "I need to learn how to get out of this mud-puddle using
    both my webbed feet and my stubby wings. By the way, I am not a duck."

    > let me make this a bit more specefic.


    Your description is already fully adequate.

    > my project is very simliar to a two level directory structures with
    > multiple files at each level.


    Yes, programmers know this task very well.

    > each directory has properties ( size , date , security ) and each file
    > has a similar properties. therefore c:\ is the root and
    > c:\a c:\b , c:\c are directories and each directory has 1 to n files.


    Yes, yes.

    > the idea is to fully describe this object and be able to traverse this
    > simple
    > tree.


    And this is a classic problem, one every programmer learns, one that you are
    learning. But no one is going to provide a solution to this homework
    assignment unless and until you post your own code, both as a sign of
    sincerity and to show prerequisite skills.

    Start by writing a routine that traverses one level, detect which of the
    nodes are directories, then ask youself how you intend to handle the
    directory entries. Then look up the work "recursion" in your Java
    programing textbook.

    Then post your code.

    However you feel about this reply, you need to realize also that people will
    come out of the woodwork to help you if you will post some code.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 5, 2004
    #9
  10. On 5 Sep 2004 00:43:58 -0700, michael wrote:

    > let me make this a bit more specefic.


    Let me make this a lot more specific.

    You would be better served by a diffent group
    at the moment, further details available here..
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    Please note carefully the last sentence, as that
    would explain why your 'problem statement' (you
    have yet to ask a question) would not be treated
    much differently there than here..

    It is much more likely you will get useful responses
    if you follow many of the suggestions Paul made,
    but I will also reiterate something I told you
    just 12 minutes after your first post.

    Ask a specific question.

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Sep 5, 2004
    #10
  11. michael

    Chris Uppal Guest

    michael wrote:

    > No this is not homework . let me make this a bit more specefic.
    > my project is very simliar to a two level directory structures with
    > multiple files at each level.


    Can you clarify something please ? It's not clear whether you are using
    the file/directory structure that is used to organise file-systems as
    an /example/ or not. It seems that the people who have replied so far
    are all assuming that you want to create a structure that mimics the
    specifics of the actual files you have on your C: drive. Maybe they are
    right, but I am wondering if you just mean that you want to know how
    to build a tree structure (with additional data), and are just using the
    file/directory hierarchy as an example of the kind of structure you want
    to build.

    -- chris
     
    Chris Uppal, Sep 5, 2004
    #11
  12. michael

    michael Guest

    Paul Lutus <> wrote in message news:<>...
    > michael wrote:
    >
    > However you feel about this reply, you need to realize also that people will
    > come out of the woodwork to help you if you will post some code.



    the actual code that I have developed is too long to post. and the
    problem is to complex to state here.

    its not difficult to traverse a tree ( 2 levels !) , the difficult
    task is to initailte a process one a node has reached a limit.

    for example

    if ( directory[0].file[10].size > 10000 ) {
    // archive , load to db , etc.. asociated with above
    };

    thanks
    Michael
     
    michael, Sep 5, 2004
    #12
  13. michael

    michael Guest

    Chris Uppal <-THIS.org> wrote in message news:<413b38f9$0$6336$>...
    > michael wrote:
    > > > No this is not homework . let me make this a bit more specefic.

    > to build a tree structure (with additional data), and are just using the
    > file/directory hierarchy as an example of the kind of structure you want
    > to build.
    >
    > -- chris


    yes chris , the directory hierarchy is used as an example. ok this is
    the real structure :

    class groups {
    int size;
    String Name;
    String networkname;
    String Capacity;
    String [] files;

    class files {
    int size;
    String Name;
    String Capacity;
    }

    then an array of groups will define an structure similiar to a two
    level
    diretory file system.

    thanks
    Michael




    }
     
    michael, Sep 6, 2004
    #13
  14. michael

    Sudsy Guest

    michael wrote:
    <snip>
    > then an array of groups will define an structure similiar to a two
    > level
    > diretory file system.


    So I must be a doofus after all. Given that file and directory
    (read: filesystem) manipulation is already provided by the
    java.io.File class, why would you want to re-invent the wheel?!
    You can find the parent directory of any file, list all files
    in a directory (hint: invoke isDirectory() first), obtain file
    size, get access time, etc.
    IOW you've already got the view of the underlying model. What
    are you trying to accomplish by adding a custom veneer?
     
    Sudsy, Sep 6, 2004
    #14
  15. michael

    Paul Lutus Guest

    michael wrote:

    > Paul Lutus <> wrote in message
    > news:<>...
    >> michael wrote:
    >>
    >> However you feel about this reply, you need to realize also that people
    >> will come out of the woodwork to help you if you will post some code.

    >
    >
    > the actual code that I have developed is too long to post.


    It shouldn't be, which is why you should post the simplest working,
    compilable version of your algorithm, then ask for help.

    > and the
    > problem is to complex to state here.


    This is definitely not true. The actual solution to your problem, suitable
    for recording an entire directory tree in your own tree, can be expressed
    in 25 lines of code.

    >
    > its not difficult to traverse a tree ( 2 levels !) , the difficult
    > task is to initailte a process one a node has reached a limit.


    Once you have defined "limit" suitably, that is not difficult at all, to any
    number of filesystem levels, but it is not part of your original problem
    statement.

    In any case, if you believe traversing a two-ply tree is not difficult, then
    post your code.

    > for example
    >
    > if ( directory[0].file[10].size > 10000 ) {
    > // archive , load to db , etc.. asociated with above
    > };


    You are adding conditions to your original problem statement. Nevertheless,
    it is not difficult. Please take my earlier advice -- start by solving the
    simplest version of the problem, and begin to read about recursion.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 6, 2004
    #15
  16. michael

    Chris Uppal Guest

    michael wrote:

    > class groups {
    > int size;
    > String Name;
    > String networkname;
    > String Capacity;
    > String [] files;
    >
    > class files {
    > int size;
    > String Name;
    > String Capacity;
    > }
    >
    > then an array of groups will define an structure similiar to a two
    > level diretory file system.


    BTW, it is almost universal to give classes names that are singular and start
    with a capital letter -- I /strongly/ recommend that you change the names to
    Group and File. Similarly, it is preferred to use lower-case names for
    ordinary fields, hence "name" and "capacity", not "Name" and "Capacity". I'll
    use the preferred forms in the rest of this post.

    I'm not really clear on what you are asking, so this may be irrelevant.

    Is there any reason you hold an array of String names of "files" in your
    groups" ? To me it would seem more natural for each Group to hold an array f
    Files. Actually a java.util.List of some kind, probably a java.util.ArrayList,
    would be better still. If you don't do that then you'll have difficulty
    mapping from the String filenames to the actual files -- the natural approach
    of using a java.util.HashMap won't work without a lot of added complication
    because the Files' names are not necessarily unique (presumably the same name
    can be used for two Files in different Groups).

    If you make that change, then it'll probably become obvious how to build and
    navigate the structure. If not then at least you should be able to describe
    /specifically/ what your problem is.

    -- chris
     
    Chris Uppal, Sep 6, 2004
    #16
  17. michael

    Jacob Guest

    michael wrote:

    > I need to write a program that mimics directory structure on PC or UNIX.
    > after loading the directory structure into such an object , I should be able to
    > print a particulr node :


    You can look at the generic directory code at
    http://geosoft.no/software/index.html#directory

    You can traverse the file system (see File and
    File.listFiles) and build Folder and DirectoryItem
    nodes as you go. To each element in the tree you
    can assocaite a back-end object which in your case
    will be the capasity-extension or whatever.

    But beware: Instantiation of sub-structures should
    be done lazily as an initial scan of a complete
    file system typically takes a considerable amount
    of time.
     
    Jacob, Sep 6, 2004
    #17
  18. michael

    michael Guest

    Jacob <> wrote in message news:<>...
    > michael wrote:
    > > I need to write a program that mimics directory structure on PC or UNIX.

    > You can look at the generic directory code at
    > http://geosoft.no/software/index.html#directory


    I appreciate you comments and suggestions. one more time, the actual
    problem has nothing to do with file/dir. It just happens that
    directory structure perfectly describes the problem.

    assumming a limited number of directories and files ( ~ 30 - 100 ),
    each node has its own properties in relationship with others. and
    therefore chaning properties of one node ( size , security , etc.. )
    should trigger other event for other nodes. for example
    if ( sizeof (node) exceeds limit ) then
    event a , event b , ..

    thanks
    Michael
    P.S : if you are not interested in this topic , please ignore it
     
    michael, Sep 6, 2004
    #18
  19. michael

    Paul Lutus Guest

    michael wrote:

    > Jacob <> wrote in message
    > news:<>...
    >> michael wrote:
    >> > I need to write a program that mimics directory structure on PC or
    >> > UNIX.

    >> You can look at the generic directory code at
    >> http://geosoft.no/software/index.html#directory

    >
    > I appreciate you comments and suggestions. one more time, the actual
    > problem has nothing to do with file/dir. It just happens that
    > directory structure perfectly describes the problem.


    It just happens that a file/directory structure exactly, precisely matches
    *your* *description* of the problem.

    > assumming a limited number of directories and files ( ~ 30 - 100 ),


    This assumption is not required.

    > each node has its own properties in relationship with others.


    This assumption is not required but can easily be accommodated.

    > and
    > therefore chaning properties of one node ( size , security , etc.. )
    > should trigger other event for other nodes.


    Did you try submitting your statement --

    "therefore chaning [sic] properties of one node ( size , security , etc.. )
    should trigger other event [sic] for other nodes"

    -- to your Java compiler? Why not? Is it because Java cannot process
    something so vague? We cannot process it either.

    If you want to solve this problem, you have to state the problem to be
    solved.

    > for example
    > if ( sizeof (node) exceeds limit ) then
    > event a , event b , ..


    State your problem. Java doesn't know how to compile a valid program from a
    series of "event a , event b , .." statements ad infinitum, and *no* *one*
    here does either.

    > P.S : if you are not interested in this topic , please ignore it


    If you are not interested in solving your problem, stop posting. There is
    zero evidence that you are even willing to state the problem to be solved.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 6, 2004
    #19
  20. michael

    Jacob Guest

    michael wrote:
    > Jacob <> wrote in message news:<>...
    >
    >>michael wrote:
    >>
    >>>I need to write a program that mimics directory structure on PC or UNIX.

    >>
    >>You can look at the generic directory code at
    >>http://geosoft.no/software/index.html#directory

    >
    >
    > I appreciate you comments and suggestions. one more time, the actual
    > problem has nothing to do with file/dir. It just happens that
    > directory structure perfectly describes the problem.


    The suggested package is not file dependent. It is just
    a data structure (similar to java.util.*) that organize
    nodes in a directory (i.e. tree) structure. You can choose
    to put whatever object you like in the nodes.
     
    Jacob, Sep 6, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Foster

    Perl script to mimic uniq

    Martin Foster, Jan 30, 2004, in forum: Perl
    Replies:
    9
    Views:
    720
    Aaron Sherman
    Feb 5, 2004
  2. Do
    Replies:
    1
    Views:
    3,012
    Ken Cox [Microsoft MVP]
    Nov 23, 2003
  3. Replies:
    6
    Views:
    2,201
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=
    May 25, 2005
  4. Mimic
    Replies:
    7
    Views:
    806
    Mimic
    Jul 14, 2005
  5. Martin
    Replies:
    4
    Views:
    395
    Jesper Madsen
    Sep 29, 2004
Loading...

Share This Page