Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Path for Temporary Directory

 
Maya Naga
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System property "java.io.tmpdir" defines the default temp location across all platforms.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True but the file will not be created in the "current" directory but in the default temporary-file directory.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*lol* Somehow I missed the createTempFile part, even though it was the largest part of the code snippet :/

Never mind.
 
Maya Naga
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maya Naga
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Maya Naga
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using File.getName()?
 
Maya Naga
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no not by getting the name...

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

 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try getAbsolutePath() instead. Next time you may want to consider going through the java.io.File API (just click on the link).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic