• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Path for Temporary Directory

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The System property "java.io.tmpdir" defines the default temp location across all platforms.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could use:
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using File.getName()?
 
Maya Naga
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic