> Programming Languages > Java
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Java



Java - "mimic directory structure , array of objects" in Programming Languages


Old 09-05-2004   #1
..cha..
 
Default mimic directory structure , array of objects

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 ***ignment 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
 
Old 09-05-2004   #2
..dr.. ..omps..
 
Default Re: mimic directory structure , array of objects

On 4 Sep 2004 16:29:37 -0700, michael wrote:
....
> any suggesions is greatly appreciated.


I suggest you ask a specific question.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Old 09-05-2004   #3
..d..
 
Default Re: mimic directory structure , array of objects

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 ***ignment 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.

 
Old 09-05-2004   #4
.... ..t..
 
Default Re: mimic directory structure , array of objects

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 ***ignment 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

 
Old 09-05-2004   #5
.... ..t..
 
Default Re: mimic directory structure , array of objects

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 ***ignment 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

 
Old 09-05-2004   #6
..d..
 
Default Re: mimic directory structure , array of objects

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

 
Old 09-05-2004   #7
.... ..t..
 
Default Re: mimic directory structure , array of objects

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 cl***es 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 ***ignment.

--
Paul Lutus
http://www.arachnoid.com

 
Old 09-05-2004   #8
..cha..
 
Default Re: mimic directory structure , array of objects

Paul Lutus <nospam@nosite.zzz> wrote in message news:<10jks6b6sok1653@corp.supernews.com>...
> 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 ***ignment 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
 
Old 09-05-2004   #9
.... ..t..
 
Default Re: mimic directory structure , array of objects

michael wrote:

> Paul Lutus <nospam@nosite.zzz> 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 cl***ic problem, one every programmer learns, one that you are
learning. But no one is going to provide a solution to this homework
***ignment 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

 
Old 09-05-2004   #10
..dr.. ..omps..
 
Default Re: mimic directory structure , array of objects

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
 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0