Prev: JPA+hibernate merge vs find fetching lazy collection
Next: Request Help - how to pass correctly formatted strings to Java in Windows
From: Daku on 30 Dec 2009 07:50 Could some Java guru please kindly help ? I am porting an application from Linux to Windows (with Active Directory running), and getting into very odd problems. I am using Eclipse IDE. I want to open a directory as: File infile = new File ("C:/temp"); ...... Then : if(infile != null) { try [ infile.createNewFile(); } catch(IOException ioe) { } if(infile != null && infile.isDirectory()) { // Do some processing } The test -- infile != null passes, but infile.isDirectory() does not. All processing inside the if block depends on infile being a directory. Any hints suggestions would be of immense help.
From: Eric Sosman on 30 Dec 2009 08:24 On 12/30/2009 7:50 AM, Daku wrote: > Could some Java guru please kindly help ? I am porting > an application from Linux to Windows (with Active > Directory running), and getting into very odd problems. > I am using Eclipse IDE. > > I want to open a directory as: > File infile = new File ("C:/temp"); > ..... > Then : > if(infile != null) Unless you've set infile=null somewhere in the "....." part, the condition is guaranteed to be true. What are you trying to test here? Do you realize that the test you're actually making says nothing at all about what is or is not on the file system? > { > try > [ ... proving that the code you've showed us cannot possibly be the code you're actually using. If the problems and remedies people point out in what you've shown don't help with the difficulty you're having, it's entirely your fault. "Doctor, I have this terrible pain in my ankle -- whenever I swallow." > infile.createNewFile(); What are you trying to do? This will (try to) create a new file if none exists already, or will leave an existing file untouched. And it will report what it did or didn't do by returning a true or false value -- but since you ignore that value, what's the point? You can be sure that a file exists, but you can also be sure that if it exists because this call created it, then the newly-created file will *not* be a directory. > } > catch(IOException ioe) > { Bad Daku! Bad! Bad! Bad! > } > > if(infile != null&& infile.isDirectory()) Again, the first part of the test is silly. The second will succeed only if the file existed before your code ran (because if it didn't, the new file you create will be an ordinary data file and not a directory). > { // Do some processing > } > > The test -- infile != null passes, but infile.isDirectory() > does not. Exactly as expected. Why do you find this surprising? > All processing inside the if block depends > on infile being a directory. > > Any hints suggestions would be of immense help. What are you trying to do? I can offer help like "Buy low, sell high" but unless I know your goal it's hard to be more specific. ... and none of this seems to have anything at all to do with Active Directory. So, again: What's your goal? -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Patricia Shanahan on 30 Dec 2009 08:39 Daku wrote: > Could some Java guru please kindly help ? I am porting > an application from Linux to Windows (with Active > Directory running), and getting into very odd problems. .... > > Any hints suggestions would be of immense help. > > Lew has dealt with a lot of the issues. I have a few supplementary comments. The Java I/O library *tries* to give you useful hints about what happened during your calls to it, through return values and exceptions. You seem to be firmly ignoring those hints. Your code is configuration dependent because it does different things based on whether a directory exists or not. You need to rethink that to make the code portable. If you really want to create c:/Temp if it does not exist, you need to change the createNewFile to mkDir. However, many users would not like an application creating a new directory at the top level. If you just want to put some files in a suitable temporary directory, I suggest using one of the createTempFile methods. The I/O library handles the problem of locating the conventional temporary file directory for you. Patricia
From: Lew on 30 Dec 2009 09:04
Patricia Shanahan wrote: > ... Eric > has dealt with a lot of the issues. But I'm flattered by the comparison. -- Lew |