Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Question

  • 01-06-2006 12:08pm
    #1
    Registered Users, Registered Users 2 Posts: 427 ✭✭


    Hey
    Could some tell me whats wrong with this code?
    Thanks
    static void PicsFolder(String FolderName) 
    {
    	String Filpath = "/pics/" + FolderName;
    	File f = new File(Filpath);
    	f.mkDir();
    }
    

    It doesnt recognise the mkDir() method.
    Thanks,
    Kev


Comments

  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    Thats cause there's no method called mkDir() in File.java. There is however a mkdir method? Everything in java is case sensitive


  • Registered Users, Registered Users 2 Posts: 427 ✭✭Kevo


    Stupid mistake. thanks.
    ok, now I have a new problem.
    static void PicsFolder(String FolderName) 
    {
    	String folder= "/pics/" + FolderName;
    	File f = new File(folder);
    	f.mkdir();
    	String thumbnails = "/pics/" + FolderName + "/thunmnails";
    	File p = new File(thumbnails);
    	p.mkdir();
    }
    
    The code doesn't do anything.
    I have called it in main but it doesnt make any folders.


  • Registered Users, Registered Users 2 Posts: 527 ✭✭✭Sean^DCT4


    Kevo wrote:
    Stupid mistake. thanks.
    ok, now I have a new problem.
    static void PicsFolder(String FolderName) 
    {
    	String folder= "/pics/" + FolderName;
    	File f = new File(folder);
    	f.mkdir();
    	String thumbnails = "/pics/" + FolderName + "/thunmnails";
    	File p = new File(thumbnails);
    	p.mkdir();
    }
    
    The code doesn't do anything.
    I have called it in main but it doesnt make any folders.


    just use my code instead
    static void PicsFolder(String FolderName) 
    {
       boolean created;
       String folder= "//pics//" + FolderName;
    
       created = new File(folder).mkdir();
    
       if ( !created ) 
            System.err.println("Directory couldnt be created....");
    
       String thumbnails = "//pics//" + FolderName + "//thumbnails";
       created = new File(thumbnails).mkdir();
       
       
       if ( !created ) 
            System.err.println("Directory couldnt be created....");
    }
    

    I think your problem was only the one / and also you have thumbnails mistyped "/thunmnails"

    EDIT:

    BTW, you can use mkdirs() instead of systematically going through each folder and creating it.

    i.e.
    if you wanted to create a folder inside a folder you'd have :
    boolean created = new File("C://newFolder1//newFolder2")[b].mkdirs();[/b]
    
    This would create 2 folders: newFolder2 inside newFolder1


  • Registered Users, Registered Users 2 Posts: 427 ✭✭Kevo


    Sean^DCT4 wrote:
    just use my code instead
    static void PicsFolder(String FolderName) 
    {
       boolean created;
       String folder= "//pics//" + FolderName;
    
       created = new File(folder).mkdir();
    
       if ( !created ) 
            System.err.println("Directory couldnt be created....");
    
       String thumbnails = "//pics//" + FolderName + "//thumbnails";
       created = new File(thumbnails).mkdir();
       
       
       if ( !created ) 
            System.err.println("Directory couldnt be created....");
    }
    

    I think your problem was only the one / and also you have thumbnails mistyped "/thunmnails"

    EDIT:

    BTW, you can use mkdirs() instead of systematically going through each folder and creating it.

    i.e.
    if you wanted to create a folder inside a folder you'd have :
    boolean created = new File("C://newFolder1//newFolder2")[b].mkdirs();[/b]
    
    This would create 2 folders: newFolder2 inside newFolder1


    Thanks thats a much better layout.
    Why do you have to use 2 // instead of one. I thought that only one was used in a filepath.

    Edit
    The folders are still not being created. I get both of the specified error messages.


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    Thge two // are not needed. You need two \\ if you are using the windows path separator.

    Firstly, are you running this on unix or windows?
    Secondly, under unix this would create a directory called /pics on the root directory. If you are running under windows you need to use a proper path, i.e. one realative to the current directory or precede it with the full pathname.

    So either do
    File f = new File("pics");
    
    to use current directory or else use a full pathname
    File f = new File("c:\\pics");
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 304 ✭✭PhantomBeaker


    If you want to avoid all the "\\" or "/" malarky, File.pathSeparator is an extremely useful thing. Although from what I remember from notes on this, "/" works in all cases.

    One other thought of why it may not work... if you are using unix, make sure your user has permission to create /pics or to write in /pics - if you're looking to use a relative directory drop the first slash... because otherwise it's trying to do that in the root directory, which you probably don't have write access to.

    Aoife


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    One other thought of why it may not work... if you are using unix, make sure your user has permission to create /pics or to write in /pics - if you're looking to use a relative directory drop the first slash... because otherwise it's trying to do that in the root directory, which you probably don't have write access to.
    This is the most likely issue, and has caused me much headaches when coding PHP.
    Does the mkdir() method throw any exceptions which you can catch and process?

    Actually, I checked, and it does:
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#mkdir()

    Add in a try...catch block to catch a SecurityException event.


Advertisement