Results 1 to 7 of 7
- 06-28-2009, 05:07 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
opening directory (folder) to reveal contents.
Hi all,
Need help with this issue I've come across. I recently learned how to create a directory. So here the promblem, I created this code for practice to get a better idea of what I can do with a directory. In the code I had a folder created on my desktop and then attempted tp have the directoy open so it would reveal its contents, but its not opening the directory (folder). It will create the folder, but just wont open it. can anyone help and tell me what Im doing wrong?
Java Code:import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.FileWriter; /** * * @author Solris */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { File data = new File("C:/Users/Solris/Desktop/data"); data.mkdir(); try { FileWriter d = new FileWriter(data); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader r = new BufferedReader(new FileReader("C:/Users/Solris/Desktop/data")); String content; while((content = r.readLine()) != null) { System.out.println(content); } } catch(Exception ex) { System.out.println("file read error"); } } }
- 06-28-2009, 06:24 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
What do you mean by opening a directory?
The directory is represented in your program by data which is an instance of File. The sorts of thing you can do with a File are described in its documentation.
As you can see from that documentation there is no operation of "opening" a directory. The nearest you get are the various list methods which return the directory's contents.
(The second catch clause should contain something more useful than "file read error". Make it a printStackTrace() like you do in the other one. Does this produce a useful runtime error message?)
- 06-28-2009, 06:38 AM #3
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
Hi pbrockway2,
Thx for ur reply to my issue. What I ment by opening the directory (folder), is to allow me to see all the files, jpg or documents that are in the folder. For example I would like the folder to open up as if I doubled clicked on it with my mouse, so it will allow me to see everthing stored in it.
If you can you provide me with an eample, I understand much better with examples, plz?:o
The link you provided me from java.sun dosen't work
- 06-28-2009, 09:41 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Java Code:import java.awt.Desktop; import java.io.File; import java.io.IOException; public class Directory { public static void main(String[] args) { String dirStr = "C:/Users/Solris/Desktop/data"; //String dirStr = "/home/pbrockway/temp"; File data = new File(dirStr); data.mkdir(); /* * "I would like the folder to open up as if I doubled clicked on it with * my mouse, so it will allow me to see everthing stored in it." * * You can't use the *File* class for this, but you might be able to * use the Desktop class. It's not guaranteed to be exactly "double click" * behaviour: for example when I ran this it opened Nautalis whereas a typical * double click would open Konqueror. Still, it's close. */ Desktop desktop = Desktop.getDesktop(); try { desktop.open(data); } catch(IOException ioe) { ioe.printStackTrace(); } /* * This is much better... It uses the File list() method to obtain * the directory's contents. In this form a Java program can actually * do something with the information. */ for(File f :data.listFiles()) { System.out.printf("%-40s (%s)%n", f, f.isFile() ? "file" : "dir"); } } }
- 06-28-2009, 09:55 AM #5
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
Hey pbrockway2,
THANK YOU!!!!:). The example you gave worked just like i wanted it too. You wouldn't believe how many forums I gone to try to get this answered. So many other where telling they didnt understand what i wanted done, or were telling me to use the getSelectedFile code, which got me no where.
you seemed to be the only one that new what i was asking for, thx again buddy:D
- 06-28-2009, 10:11 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
You're welcome.
- 06-28-2009, 06:40 PM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Just to throw it something in here...
JFileChooser works well too... Shows up with a graphical representation of files, and there's plenty you can do with it to customize its appearanceIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
Opening the Application
By jasmu in forum Advanced JavaReplies: 6Last Post: 04-22-2009, 06:21 AM -
problem in opening new url from JSP
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-02-2009, 06:46 AM -
Opening in Java and .Net
By techinvo in forum Jobs OfferedReplies: 0Last Post: 03-22-2009, 07:24 AM -
Opening URLConnection
By Java Tip in forum Java TipReplies: 0Last Post: 11-24-2007, 07:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks