mimic directory structure , array of objects

M

michael

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
 
S

Sudsy

michael said:
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.
 
P

Paul Lutus

michael said:
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.
 
P

Paul Lutus

Sudsy said:
michael said:
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.
 
S

Sudsy

Paul said:
Sudsy said:
michael wrote:
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
 
P

Paul Lutus

Sudsy said:
Paul said:
Sudsy said:
michael wrote:
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.
 
M

michael

Paul Lutus said:
michael said:
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
 
P

Paul Lutus

michael said:
Paul Lutus <[email protected]> wrote in message

/ ...
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.
 
A

Andrew Thompson

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
 
C

Chris Uppal

michael said:
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
 
M

michael

Paul Lutus said:
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
 
M

michael

Chris Uppal said:
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




}
 
S

Sudsy

michael wrote:
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?
 
P

Paul Lutus

michael said:
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.
 
C

Chris Uppal

michael said:
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
 
J

Jacob

michael said:
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.
 
M

michael

Jacob said:
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
 
P

Paul Lutus

michael said:
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.
 
J

Jacob

michael said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top