Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2009, 06:07 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
solris is on a distinguished road
Default 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?

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");

}
     }
    }
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-28-2009, 07:24 AM
Senior Member
 
Join Date: Feb 2009
Posts: 629
Rep Power: 2
pbrockway2 is on a distinguished road
Default
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?)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-28-2009, 07:38 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
solris is on a distinguished road
Red face
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?

The link you provided me from java.sun dosen't work
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-28-2009, 10:41 AM
Senior Member
 
Join Date: Feb 2009
Posts: 629
Rep Power: 2
pbrockway2 is on a distinguished road
Default
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");
        }
    }
}
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-28-2009, 10:55 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
solris is on a distinguished road
Talking
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-28-2009, 11:11 AM
Senior Member
 
Join Date: Feb 2009
Posts: 629
Rep Power: 2
pbrockway2 is on a distinguished road
Default
You're welcome.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-28-2009, 07:40 PM
Senior Member
 
Join Date: Mar 2009
Posts: 392
Rep Power: 2
Singing Boyo is on a distinguished road
Default
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 appearance
__________________
If 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!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
opening folder, openingrectory

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read file from directory, update contents of the each file svpriyan New To Java 2 05-11-2009 11:07 AM
Opening the Application jasmu Advanced Java 6 04-22-2009 07:21 AM
problem in opening new url from JSP rakesh_n_mehta JavaServer Pages (JSP) and JSTL 0 04-02-2009 07:46 AM
Opening in Java and .Net techinvo Jobs Offered 0 03-22-2009 08:24 AM
Opening URLConnection Java Tip Java Tips 0 11-24-2007 08:37 PM


All times are GMT +2. The time now is 11:04 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org