Results 1 to 6 of 6

Thread: .exists URGENT

  1. #1
    joolske is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default .exists URGENT

    Hi!

    I'm trying to create a file before writing/reading to/from it. I always get an error that the package (although it is a file) doesn't exist. My code is as follows:

    File myFile = new File(System.getProperty("user.dir") + File.separator
    + "\\output\\Daganalyse.pdf");
    File parentDir = myFile.getParentFile();
    if(parentDir.isDirectory())
    {
    parentDir.mkdirs();
    }

    (it turns red at the 'if')

    Please help me!
    Thank you!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: .exists URGENT

    What turns 'red at the if'? Nothing else? You must be using a lousy IDE.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    joolske is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: .exists URGENT

    Netbeans gives error at the 'if':
    illegal start of type
    package parentDir does not exist
    <identifier> expected
    ';' expected

    thanks for the quick response!

  4. #4
    joolske is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: .exists URGENT

    and of course, the correct code is: if (!parentDir.isDirectory()) (and I have the same problem with .exists), but it still turns red

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: .exists URGENT

    Are those statements part of a method body?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    joolske is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: .exists URGENT

    I've already been able to solve the problem by using different coding:

    Java Code:
    static String datum = "14-13-2012";
       static String pathDir = "H:\\Weekanalyses";
       //static String pathFile = "H:\\Weekanalyses\\newfile" + datum +".txt";
       static int i =0;
       static String pathFile = "H:\\ Leveranciers\\GoedkoopsteLeveranciers-" + i + ".txt";
       
       
        public static void main(String[] args) 
        {File f = new File(pathDir);
        try{
            if(f.exists()==false){
                 f.mkdir();
            System.out.println("Directory Created");
              }
              else{
             System.out.println("Directory is not created");
            }
         }catch(Exception e){
             e.printStackTrace();
           }
        
        try {
     
    	      File file = new File(pathFile);
     
    	      if (file.createNewFile()){
    	        System.out.println("File is created!");
    	      }else{
    	        System.out.println("File already exists.");
    	      }
     
        	} catch (IOException e) {
    	      e.printStackTrace();
    	}
    Thank you for your help!
    Last edited by quad64bit; 12-12-2012 at 04:26 PM. Reason: Added code tags

Similar Threads

  1. Help with knowing if .split()[1] exists
    By trath in forum New To Java
    Replies: 1
    Last Post: 08-29-2012, 09:28 PM
  2. do set and get methods really exists?
    By ronnie in forum New To Java
    Replies: 3
    Last Post: 02-22-2012, 06:07 PM
  3. Determining which choice exists
    By kjkrum in forum XML
    Replies: 1
    Last Post: 12-16-2011, 12:27 AM
  4. How do I check if a database exists ...Any help?
    By nmvictor in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 04:21 PM
  5. [URGENT]SHA Encryption System...need urgent helps
    By java_idiot in forum New To Java
    Replies: 6
    Last Post: 05-02-2010, 10:04 AM

Posting Permissions

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