Results 1 to 7 of 7
  1. #1
    solris is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    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?

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    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?)

  3. #3
    solris is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    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?:o

    The link you provided me from java.sun dosen't work

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

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

  5. #5
    solris is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    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:D

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    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!

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  2. Opening the Application
    By jasmu in forum Advanced Java
    Replies: 6
    Last Post: 04-22-2009, 06:21 AM
  3. problem in opening new url from JSP
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-02-2009, 06:46 AM
  4. Opening in Java and .Net
    By techinvo in forum Jobs Offered
    Replies: 0
    Last Post: 03-22-2009, 07:24 AM
  5. Opening URLConnection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-24-2007, 07:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •