[groovy-user] Creating a Temporary Directory

1,052 views
Skip to first unread message

AwTIn

unread,
Jul 31, 2009, 8:52:49 AM7/31/09
to us...@groovy.codehaus.org

Using *.mkdir() works fine for creating a standard directory, but, even after
various Internet searches, I can't seem to be able to create a temporary
directory.

I've tried:

def jobDir = File.createTempFile('sequence_job', new
File('/home/bas/kluthta/cylofold_jobs/'))
jobDir.mkdir()

Any suggestions?

--
View this message in context: http://www.nabble.com/Creating-a-Temporary-Directory-tp24755903p24755903.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Andreas Jöcker

unread,
Jul 31, 2009, 8:55:35 AM7/31/09
to us...@groovy.codehaus.org

> def jobDir = File.createTempFile('sequence_job', new
> File('/home/bas/kluthta/cylofold_jobs/'))
> jobDir.mkdir()
>
>
you are using a wrong method call... createTempFile either takes two
Strings (prefix / suffix) or two Strings + a File object (the temp
directory).

do I miss something - does this compile ?


--

Andreas Jöcker
GiS - Gesellschaft für integrierte Systemplanung mbH
Junkersstr. 2
69469 Weinheim

E-Mail a.jo...@gis-systemhaus.de
Telefon +49 6201 503-59
Fax +49 6201 503-66

Gesellschaft für integrierte Systemplanung mbH
Geschäftsführer: Eckhard Haffmann, Alfred Gai, Bernd Heselmann
Sitz der Gesellschaft: Zeppelinstr. 11 - 91052 Erlangen
Amtsgericht Fürth/Bayern - Handelsregister-Nr. HRB 3435

Robert Fischer

unread,
Jul 31, 2009, 8:56:10 AM7/31/09
to us...@groovy.codehaus.org
That's actually creating a regular file. Trying to make a directory on top of it isn't going to
work. I suppose you could delete it and then create a directory with the same name...?

~~ Robert.

AwTIn wrote:
> Using *.mkdir() works fine for creating a standard directory, but, even after
> various Internet searches, I can't seem to be able to create a temporary
> directory.
>
> I've tried:
>
> def jobDir = File.createTempFile('sequence_job', new
> File('/home/bas/kluthta/cylofold_jobs/'))
> jobDir.mkdir()
>
> Any suggestions?
>

--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

AwTIn

unread,
Jul 31, 2009, 9:00:18 AM7/31/09
to us...@groovy.codehaus.org

The typical syntax for creating a temp file is: File.createTempFile('name',
'.extension'), but it is optional to add "new File('directory')" to specify
where the file will be created. Yes, it compiles.

--
View this message in context: http://www.nabble.com/Creating-a-Temporary-Directory-tp24755903p24756001.html


Sent from the groovy - user mailing list archive at Nabble.com.

Tom Nichols

unread,
Jul 31, 2009, 9:05:51 AM7/31/09
to us...@groovy.codehaus.org
I've used something like this in the past:

new File( File.createTempFile(...).parentFile(), 'someDir' ).mkdir()

Although your use of File.createTempFile is odd; that method takes two
strings, (a file name prefix and suffix) -- you're passing a prefix
and a File instance which points to a directory. createTempFile
specifies that the method creates an empty file, so you cannot call
"mkdir()" on the resulting File instance to create it as a directory.
This is very different from new File(..) which just creates a
'descriptor' of sorts, which may or may not represent an actual file
or directory on the filesystem.

-Tom

Robert Fischer

unread,
Jul 31, 2009, 9:09:58 AM7/31/09
to us...@groovy.codehaus.org
I have no doubt it compiles: remember, it's Groovy.

~~ Robert.

Andreas Jöcker wrote:
>> def jobDir = File.createTempFile('sequence_job', new
>> File('/home/bas/kluthta/cylofold_jobs/'))
>> jobDir.mkdir()
>>
>>
> you are using a wrong method call... createTempFile either takes two
> Strings (prefix / suffix) or two Strings + a File object (the temp
> directory).
>
> do I miss something - does this compile ?
>
>

--

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------

Andreas Jöcker

unread,
Jul 31, 2009, 9:21:24 AM7/31/09
to us...@groovy.codehaus.org
in groovy compiling doesnt mean correct working ;-) so ok it compiles,
but still the useage is wrong.

afaik groovy doesnt offer any additional File.createTempFile construct
(at least not shown in the docs) - and your construct is not java correct !

def jobDir = File.createTempFile('sequence_job', '.temp', new
File('/home/bas/kluthta/cylofold_jobs/'))
jobDir.mkdir()

would fit

AwTIn

unread,
Jul 31, 2009, 9:23:41 AM7/31/09
to us...@groovy.codehaus.org

Sorry, I am rather new to Groovy (and Java, for that matter0. What exactly
do you mean by ".parentFile()"?

--
View this message in context: http://www.nabble.com/Creating-a-Temporary-Directory-tp24755903p24756337.html

AwTIn

unread,
Jul 31, 2009, 9:24:29 AM7/31/09
to us...@groovy.codehaus.org

I have also tried that syntax. Doesn't work.

--
View this message in context: http://www.nabble.com/Creating-a-Temporary-Directory-tp24755903p24756351.html


Sent from the groovy - user mailing list archive at Nabble.com.

Andreas Jöcker

unread,
Jul 31, 2009, 9:26:20 AM7/31/09
to us...@groovy.codehaus.org

> I have also tried that syntax. Doesn't work.

but now its aleast a correct syntax....

> Sorry, I am rather new to Groovy (and Java, for that matter0. What exactly
> do you mean by ".parentFile()"?

http://java.sun.com/javase/6/docs/api/java/io/File.html#getParentFile()


--

Andreas Jöcker


GiS - Gesellschaft für integrierte Systemplanung mbH
Junkersstr. 2
69469 Weinheim

Gesellschaft für integrierte Systemplanung mbH
Geschäftsführer: Eckhard Haffmann, Alfred Gai, Bernd Heselmann
Sitz der Gesellschaft: Zeppelinstr. 11 - 91052 Erlangen
Amtsgericht Fürth/Bayern - Handelsregister-Nr. HRB 3435

AwTIn

unread,
Jul 31, 2009, 9:37:49 AM7/31/09
to us...@groovy.codehaus.org

I have tried this:

new File( File.createTempFile('sequence_job',
'.dir').parentFile(), '/home/bas/kluthta/cylofold_jobs/' ).mkdir()

I am getting a runtime exception after compilation. Am I missing something?

--
View this message in context: http://www.nabble.com/Creating-a-Temporary-Directory-tp24755903p24756580.html

Robert Fischer

unread,
Jul 31, 2009, 9:47:45 AM7/31/09
to us...@groovy.codehaus.org
Specifically, what do you think you're telling that code to do?

Take a more careful read-through of the java.io.File API: you're not quite grokking how it works.

~~ Robert.

--

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------

Tom Nichols

unread,
Jul 31, 2009, 9:49:09 AM7/31/09
to us...@groovy.codehaus.org
ah, that should be ".parentFile" or ".getParentFile()" (since they're
synonymous) -
http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#getParentFile()

Also, if you're trying to create a temp dir in the given dir you're
listing as the second argument, you probably want a different
approach. As someone else mentioned, you probably want to create the
unique temp file, then delete it and re-create as a directory. So do
like this:

File baseDir = new File( '/home/bas/kluthta/cylofold_jobs/' )
File dir = File.createTempFile( 'sequence_job', '.dir', baseDir )
dir.delete() // delete the file that was created
dir.mkdir() // create a directory in its place.

Luke Daley

unread,
Jul 31, 2009, 9:51:27 PM7/31/09
to us...@groovy.codehaus.org
For the ldap-server plugin I do the following:

import org.springframework.web.util.WebUtils
import org.codehaus.groovy.grails.web.context.ServletContextHolder
import grails.util.BuildSettingsHolder

def tmp = ServletContextHolder.servletContext ?
WebUtils.getTempDir(ServletContextHolder.servletContext) :
BuildSettingsHolder.settings.projectWorkDir

ServletContextHolder.servletContext is null during tests.

Reply all
Reply to author
Forward
0 new messages