wood burning stoves 2.0*
The moose likes Java in General and the fly likes Path for Temporary Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Path for Temporary Directory" Watch "Path for Temporary Directory" New topic
Author

Path for Temporary Directory

Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
hi ranches....

Am developing a program to compress and decompress the dicom files.I know that we can create a directory to store compressed files automatically using mkdir...
But my question is if user forgot to select the destination folder for storing the files it should automatically stored in temp directories (such as temp in windows operating system) how to do that...

my code is as follows:


here is where i store the files when user forgot to select destination directory.
but i need to change path which suits for all operating system.

thanks in advance
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

The System property "java.io.tmpdir" defines the default temp location across all platforms.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or you could use:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Wouter Oet wrote:Or you could use:

Depends; the user running the application may not have access to the "current" directory, or if it's a webapp deployed from a WAR, there's nowhere to create a physical file.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

True but the file will not be created in the "current" directory but in the default temporary-file directory.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

*lol* Somehow I missed the createTempFile part, even though it was the largest part of the code snippet :/

Never mind.
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
Thanks for your idea's ranches..

I tried using:
File f=File.createTempFile("temp",null);
it works fine...

But my doubts are:

1) whether i can create a folder inside that tmp directory( which is available in all OS) rather than storing alone the files commonly?
2) i do not find java.io.tmdir package in my system.

Am using both open suse and windows xp



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

I once created a temp folder as follows:
Of course that will not work if you have no rights to delete files in the temp folder but usually that's not the case.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
Thanks rob..

Actually i used the below method to store the files that is to be compressed..




with the help of your idea i can change it as:


but where can i add the filename which i gave as sample +k(0,1,2,3...) in my previous code.....


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

This way you are creating a new unique temporary folder each time. Is that what you want? I doubt it.

Move the creation of the folder outside of the loop, then inside the loop use that folder to create new files:
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
i solved the temporary location by using your idea.. below is the code i used..


next thing am doing is getting the destination selected by the user and storing the compressed files..
i used getSelectedFile() method to get the path, but i don know how to give name for the selected files (for example, 'sample +k' in above code)

can you give me some idea on how to do it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Ambika Nagalakshmi wrote:2) i do not find java.io.tmdir package in my system.

It's not a package, it's a system property (and it's called java.io.tmpdir, you forgot the p). You can use it like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
ya i solved that ..
now my doubt is how to give name for the compressing files...

for example,sample(or dicom) is the name given to the files that is to be compressed,'k' will be from 0 to n... so sample 0, sample 1, sample2 etc..


i used getSelectedFile() method to get the path from the user , but i don know how to give name for the selected files
what i want to do to get the name at run time?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Using File.getName()?
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
no not by getting the name...

if you view these codes i think you can understand what is my doubt:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Try getAbsolutePath() instead. Next time you may want to consider going through the java.io.File API (just click on the link).
 
 
subject: Path for Temporary Directory
 
Similar Threads
Relative path issue with ubuntu
Cannot compile a java program
How to use JFileChooser to select directories only ??
Character encoding
Need Design Help : Temp Directory and Context Path