boatful
asked on
getting top level directory structure with applet
Is it possible for a JAVA applet, embedded in an HTML page with security privileges granted, to identify the drive letters for each top level directory?
Does anyone have source code I could cut and paste? I'm pretty new to JAVA programming. I know how to request/grant I/O privileges already.
Thanks in advance!!
Tony
Does anyone have source code I could cut and paste? I'm pretty new to JAVA programming. I know how to request/grant I/O privileges already.
Thanks in advance!!
Tony
I think in JDK 1.2 and up use java.io.File.listRoots() , I am not sure.
Hi
Use FileSystemView class and from that class use method
abstract File[] getRoots()
This returns all root partitians on the system.
Rajiv
Use FileSystemView class and from that class use method
abstract File[] getRoots()
This returns all root partitians on the system.
Rajiv
That is correct, Ash. The static method listRoots() was introduced in Java 1.2. It will return an array of File objects identifying the drives of the system. The method does not throw security exceptions. If a security manager exists and the user is denied access to a root directory then it will not be included in the result.
However, before Java 1.2 (and still since), the javax.swing.filechooser.Fi leSystemVi ew class provides similar behavior in its getRoots() method. It also will return an array of File objects. To invoke it, you need to get the singleton FileSystemView object first by calling FileSystemView.getFileSyst emView(). This method does not declare that it throws any exceptions either.
Best regards,
Jim Cakalic
However, before Java 1.2 (and still since), the javax.swing.filechooser.Fi
Best regards,
Jim Cakalic
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Huh? How does ExampleFileView help in getting the root directories of the system?
Here is the code for Java 1.2 (and up):
import java.io.File;
...
File[] roots = File.listRoots();
With the swing release prior to Java 1.2
// maybe com.sun.java.swing?
import javax.swing.filechooser.Fi leSystemVi ew;
...
File[] roots = FileSystemView.getFileSyst emView().g etRoots();
Jim
import java.io.File;
...
File[] roots = File.listRoots();
With the swing release prior to Java 1.2
// maybe com.sun.java.swing?
import javax.swing.filechooser.Fi
...
File[] roots = FileSystemView.getFileSyst
Jim
Copy&Paste
public File[] getRoots() {
Vector rootsVector = new Vector();
// Create the A: drive whether it is mounted or not
WindowsFloppy floppy = new WindowsFloppy();
rootsVector.addElement(flo ppy);
// Run through all possible mount points and check
// for their existance.
for (char c = 'C'; c <= 'Z'; c++) {
char device[] = {c, ':', '\\'};
String deviceName = new String(device);
File deviceFile = new File(deviceName);
if (deviceFile != null && deviceFile.exists()) {
rootsVector.addElement(dev iceFile);
}
}
File[] roots = new File[rootsVector.size()];
rootsVector.copyInto(roots );
return roots;
}
/**
* Fake the floppy drive. There is no way to know whether
* it is mounted or not, and doing a file.isDirectory or
* file.exists() causes Windows to pop up the "Insert Floppy"
* dialog. We therefore assume that A: is the floppy drive,
* and force it to always return true for isDirectory()
*/
class WindowsFloppy extends File {
public WindowsFloppy() {
super("A" + ":" + "\\");
}
public boolean isDirectory() {
return true;
};
}
public File[] getRoots() {
Vector rootsVector = new Vector();
// Create the A: drive whether it is mounted or not
WindowsFloppy floppy = new WindowsFloppy();
rootsVector.addElement(flo
// Run through all possible mount points and check
// for their existance.
for (char c = 'C'; c <= 'Z'; c++) {
char device[] = {c, ':', '\\'};
String deviceName = new String(device);
File deviceFile = new File(deviceName);
if (deviceFile != null && deviceFile.exists()) {
rootsVector.addElement(dev
}
}
File[] roots = new File[rootsVector.size()];
rootsVector.copyInto(roots
return roots;
}
/**
* Fake the floppy drive. There is no way to know whether
* it is mounted or not, and doing a file.isDirectory or
* file.exists() causes Windows to pop up the "Insert Floppy"
* dialog. We therefore assume that A: is the floppy drive,
* and force it to always return true for isDirectory()
*/
class WindowsFloppy extends File {
public WindowsFloppy() {
super("A" + ":" + "\\");
}
public boolean isDirectory() {
return true;
};
}
Hi All,
Just Run the programe given above u will find everything what u want...
Ur all doubts will be cleared...
Rajiv
Just Run the programe given above u will find everything what u want...
Ur all doubts will be cleared...
Rajiv
heyhey,
that's a neat way of doing it!
that's a neat way of doing it!
well ... swing sources :)
ASKER
Heyhey,
This seems much more straightforward than Terajiv's answer. I have yet to compile/test it. I have 1 question:
Do I have to worry about the CD-ROM drive as well as the floppy? I don't want folks to mistakenly get the impression they can write to it.
Thanks everyone for your advise on this one!!
Tony
This seems much more straightforward than Terajiv's answer. I have yet to compile/test it. I have 1 question:
Do I have to worry about the CD-ROM drive as well as the floppy? I don't want folks to mistakenly get the impression they can write to it.
Thanks everyone for your advise on this one!!
Tony
ASKER
I am nearly there I think...
Here's the source:
import java.io.File;
import java.applet.Applet;
import java.util.Vector;
public class getroots extends Applet
{
public File[] getRoots() {
Vector rootsVector = new Vector();
// Create the A: drive whether it is mounted or not
WindowsFloppy floppy = new WindowsFloppy();
rootsVector.addElement(flo ppy);
// Run through all possible mount points and check
// for their existance.
for (char c = 'C'; c <= 'Z'; c++) {
char device[] = {c, ':', '\\'};
String deviceName = new String(device);
File deviceFile = new File(deviceName);
if (deviceFile != null && deviceFile.exists()) {
rootsVector.addElement(dev iceFile);
}
}
File[] roots = new File[rootsVector.size()];
rootsVector.copyInto(roots );
return roots;
}
class WindowsFloppy extends File {
public WindowsFloppy() {
super("A" + ":" + "\\");
}
public boolean isDirectory() {
return true;
};
}
}
Here's the HTML source:
<html>
<head>
<title>Get all Roots</title>
<applet code="getroots.class" name="top_dirs" width=1 height=1></applet>
</head>
<body>
<a href="javascript:alert(top _dirs)">Ge t all Roots</a>
</body>
</html>
Here's the response:
getroots[panel16,0,0,1x1,l ayout=java .awt.FlowL ayout]
But how do I call getroots.class in order to get back the driveletter array?????
Tony
Here's the source:
import java.io.File;
import java.applet.Applet;
import java.util.Vector;
public class getroots extends Applet
{
public File[] getRoots() {
Vector rootsVector = new Vector();
// Create the A: drive whether it is mounted or not
WindowsFloppy floppy = new WindowsFloppy();
rootsVector.addElement(flo
// Run through all possible mount points and check
// for their existance.
for (char c = 'C'; c <= 'Z'; c++) {
char device[] = {c, ':', '\\'};
String deviceName = new String(device);
File deviceFile = new File(deviceName);
if (deviceFile != null && deviceFile.exists()) {
rootsVector.addElement(dev
}
}
File[] roots = new File[rootsVector.size()];
rootsVector.copyInto(roots
return roots;
}
class WindowsFloppy extends File {
public WindowsFloppy() {
super("A" + ":" + "\\");
}
public boolean isDirectory() {
return true;
};
}
}
Here's the HTML source:
<html>
<head>
<title>Get all Roots</title>
<applet code="getroots.class" name="top_dirs" width=1 height=1></applet>
</head>
<body>
<a href="javascript:alert(top
</body>
</html>
Here's the response:
getroots[panel16,0,0,1x1,l
But how do I call getroots.class in order to get back the driveletter array?????
Tony
Hey My Program Gives everything u want... See u need Roots and u get Everything including Roots...
Infact this is the Best Example for Getting roots... I have taken this example from java's Examples...
Infact this is the Best Example for Getting roots... I have taken this example from java's Examples...
ASKER
Terajiv,
It looks like in your answer code I need to first provide a file name. I don't see how this helps me determine the top level directory structure of the client's system.
Unless you can explain how I can call this applet and in return get the root structure (i.e. A:,C:,D:,S:,etc) I will need to reject your answer.
Tony
It looks like in your answer code I need to first provide a file name. I don't see how this helps me determine the top level directory structure of the client's system.
Unless you can explain how I can call this applet and in return get the root structure (i.e. A:,C:,D:,S:,etc) I will need to reject your answer.
Tony
Hi boatful,
See this Program gets all available roots. i.e. available drives. along with that it gets files also those having a single dot in between.
Now if ur using applets then u can put one button clicking which will invoke another applet or if ur using browser then either on same Browser or new Browser. This u can invoke in actionPerformed method.
Dont u think its easy? the code is availble here so use it. If u want more codes u can always use Java sample codes. those r really good...
I hope ur doubt is cleared...
Have A Nice Time
Rajiv
See this Program gets all available roots. i.e. available drives. along with that it gets files also those having a single dot in between.
Now if ur using applets then u can put one button clicking which will invoke another applet or if ur using browser then either on same Browser or new Browser. This u can invoke in actionPerformed method.
Dont u think its easy? the code is availble here so use it. If u want more codes u can always use Java sample codes. those r really good...
I hope ur doubt is cleared...
Have A Nice Time
Rajiv
ASKER
Rajiv,
I really need more direction than:
"This u can invoke in actionPerformed method".
For example, in an HTML form:
<FORM>
<INPUT TYPE="BUTTON" VALUE="Get Root Heirarchy" OnClick="???????????????">
</FORM>
Please fill in the ??????????????????
Thanks,
Tony
I really need more direction than:
"This u can invoke in actionPerformed method".
For example, in an HTML form:
<FORM>
<INPUT TYPE="BUTTON" VALUE="Get Root Heirarchy" OnClick="???????????????">
</FORM>
Please fill in the ??????????????????
Thanks,
Tony
Ok. I'm a little slow but I finally figured out whats going on here with ExampleFileView. If you add the following method to the code that terajiv posted you should be able to compile and run the application.
public static void main(String[] args) {
JFrame frame = new JFrame("File chooser");
JFileChooser chooser = new JFileChooser();
FileView fileView = new ExampleFileView();
chooser.setFileView(fileVi ew);
frame.getContentPane().add (chooser);
frame.pack();
frame.show();
}
It will display a JFileChooser. When you drop drown the combobox labeled "Look in:", it will present a number of options including what appear to be the valid drives on the system. My combobox has A:, C:, and D:. Your contents will vary.
Our contention, terajiv, is that this behavior of JFileChooser has little or nothing to do with the code that you posted. I can get exactly the same functionality by removing the two lines above that reference a fileview. When I run the program, it works exactly the same.
The reason for this is that FileView is intended to be a hook into JFileChooser to allow you to provide icons and type descriptions for files and extensions that override those provided by the current look and feel. By definition, JFileChooser still uses the default L&F FileView implementation to get information it needs whenever the user-provided FileView methods return null.
In summary, the behavior of determining the root drives of the system is not provided by your ExampleFileView class but by JFileChooser. Your class is a way of customizing how JFileChooser will present that information. If boatful wants to determine this information without using a JFileChooser, he cannot do so using your class.
Best regards,
Jim Cakalic
public static void main(String[] args) {
JFrame frame = new JFrame("File chooser");
JFileChooser chooser = new JFileChooser();
FileView fileView = new ExampleFileView();
chooser.setFileView(fileVi
frame.getContentPane().add
frame.pack();
frame.show();
}
It will display a JFileChooser. When you drop drown the combobox labeled "Look in:", it will present a number of options including what appear to be the valid drives on the system. My combobox has A:, C:, and D:. Your contents will vary.
Our contention, terajiv, is that this behavior of JFileChooser has little or nothing to do with the code that you posted. I can get exactly the same functionality by removing the two lines above that reference a fileview. When I run the program, it works exactly the same.
The reason for this is that FileView is intended to be a hook into JFileChooser to allow you to provide icons and type descriptions for files and extensions that override those provided by the current look and feel. By definition, JFileChooser still uses the default L&F FileView implementation to get information it needs whenever the user-provided FileView methods return null.
In summary, the behavior of determining the root drives of the system is not provided by your ExampleFileView class but by JFileChooser. Your class is a way of customizing how JFileChooser will present that information. If boatful wants to determine this information without using a JFileChooser, he cannot do so using your class.
Best regards,
Jim Cakalic
ASKER
Hi:
Are you saying the code could be as simple as:
public static void main(String[] args) {
JFrame frame = new JFrame("File chooser");
JFileChooser chooser = new JFileChooser();
frame.getContentPane().add (chooser);
frame.pack();
frame.show();
}
It would be ideal if the class returned an array of drive letters, such as [a:,b:,d:) etc.
Boatful (Tony Shaw)
Are you saying the code could be as simple as:
public static void main(String[] args) {
JFrame frame = new JFrame("File chooser");
JFileChooser chooser = new JFileChooser();
frame.getContentPane().add
frame.pack();
frame.show();
}
It would be ideal if the class returned an array of drive letters, such as [a:,b:,d:) etc.
Boatful (Tony Shaw)
No, Tony. What I am saying is that JFileChooser knows how to determine the file system roots. This is the gist of my initial post on this topic. JFileChooser uses a concrete implementation of javax.swing.filechooser.Fi leSystemVi ew to, in the words of the jdk documentation, 'intuit as much OS specific file system information as possible'.
The main method I posted was simply to demonstrate how ExampleFileView was to be used and show that JFileChooser's ability to obtain file system roots is independent of that class. It is not what you want if you need to determine file system roots independent of any mechanism for interacting with the user (like JFileChooser). You can use any of the other several methods that have been described:
1) The code as posted by heyhey.
2) java.io.File.listRoots() as suggested by Ash (Java 1.2).
3) javax.swing.filechooser.Fi leSystemVi ew.getRoot s() as suggested by terajiv (requires swing).
As to how these might be called from JavaScript, someone more familiar with programming applets will have to help your there.
Best regards,
Jim Cakalic
The main method I posted was simply to demonstrate how ExampleFileView was to be used and show that JFileChooser's ability to obtain file system roots is independent of that class. It is not what you want if you need to determine file system roots independent of any mechanism for interacting with the user (like JFileChooser). You can use any of the other several methods that have been described:
1) The code as posted by heyhey.
2) java.io.File.listRoots() as suggested by Ash (Java 1.2).
3) javax.swing.filechooser.Fi
As to how these might be called from JavaScript, someone more familiar with programming applets will have to help your there.
Best regards,
Jim Cakalic
Hi boatful,
I told u my friend that I have taken this Example from Documents...
Anyway... U can also go for FileDialog Class this u can use it...
Gives the similar results...
Rajiv
I told u my friend that I have taken this Example from Documents...
Anyway... U can also go for FileDialog Class this u can use it...
Gives the similar results...
Rajiv
well - do we really need Swing to get list of available drives ? :)
probably somebody have enough time to go through the Swing sources and extract the appropriate piece of code (I just made a quick Copy&Paste)
probably somebody have enough time to go through the Swing sources and extract the appropriate piece of code (I just made a quick Copy&Paste)
ASKER
Thanks All!
Again, my feeble brain prefers the cleanest/simplest implementation, by HEYHEY.
I'd like to use it, but again, when I call the applet:
<applet code="getroots.class" name="top_getDirs_applet" width=1 height=1></applet>
from a web page in JavaScript:
alert(top_getDirs_applet);
the applet returns:
getroots[panel16,0,0,1x1,l ayout=java .awt.FlowL ayout]
not the drive letters themselves.
What do I need to do differently??
Tony
Again, my feeble brain prefers the cleanest/simplest implementation, by HEYHEY.
I'd like to use it, but again, when I call the applet:
<applet code="getroots.class" name="top_getDirs_applet" width=1 height=1></applet>
from a web page in JavaScript:
alert(top_getDirs_applet);
the applet returns:
getroots[panel16,0,0,1x1,l
not the drive letters themselves.
What do I need to do differently??
Tony
>> alert(top_getDirs_applet);
you need to call method on that applet and analyze the result - not access the applet (top_getDirs_applet) directly.
you need to call method on that applet and analyze the result - not access the applet (top_getDirs_applet) directly.
ASKER
Oh, yeah!
Sorry I wasn't thinking. I will try again!
In the example above, it would be something like:
alert(new String(top_getDirs_applet. getRoots)) ;
Tony
Sorry I wasn't thinking. I will try again!
In the example above, it would be something like:
alert(new String(top_getDirs_applet.
Tony
ASKER
This gives a JavaScript error too, when I try calling top_getDirs_applet.getRoot s
I probably need to amend the applet to first establish I/O privileges through the security manager. I know how to do this, but I left it out up til now.
Tony
I probably need to amend the applet to first establish I/O privileges through the security manager. I know how to do this, but I left it out up til now.
Tony
So, Tony, did you have an answer to the problem you posted?
ASKER
Jim,
I never tried compiling the answer from terajiv, as it seems to reach far beyond my limited needs.
I compiled HEYHEY's suggestion (see my comment of 4/18), but still didn't ever get it to return the drive letter names (see my comment of 4/25). Since then I've been too busy to re-compile it in a form which first establishes I/O privileges. I'll put it on top of my todo list and post another comment...
Tony
I never tried compiling the answer from terajiv, as it seems to reach far beyond my limited needs.
I compiled HEYHEY's suggestion (see my comment of 4/18), but still didn't ever get it to return the drive letter names (see my comment of 4/25). Since then I've been too busy to re-compile it in a form which first establishes I/O privileges. I'll put it on top of my todo list and post another comment...
Tony
hey boatful.... try my example Why dont u try...??? Try it and see the result...
Ur all Ques. will be solved in that...
Rajiv
Ur all Ques. will be solved in that...
Rajiv
hey boatful.... try my example Why dont u try...??? Try it and see the result...
Ur all Ques. will be solved in that...
Rajiv
Ur all Ques. will be solved in that...
Rajiv