Link to home
Start Free TrialLog in
Avatar of Baha
Baha

asked on

getTempDir() method

Hi all,
Is there anyone that could give me a good website where to find the getTempDir() method with its class easy to download?
This method is supposed to get the local temporary directory. I couldn't find it in the Java SDK 1.4
but looking for it on the web, i found that some people already created some class with such a method included in. For example there is a class called org.apache.commons.httpclient.methods.GetMethod that has the getTempDir() method. Unfortunately, i couldn't find any good spot where to download such a class...
Thanks in advance for you help!!
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and where can we get a list of valid system properties to pass to that method?
Do you mean how can you find out what system properties are available? If so, run this class without parameters:

public class Props {

  public static void main(String[] args){
    if(args.length > 0){
     for(int i = 0;i < args.length;i++){
      System.out.println(System.getProperty(args[0]));
     }
    }
    else {
      System.getProperties().list(System.out);
    }
  }
}
thanks!
Avatar of Venci75
Venci75

If you need to create a temporary file, use:
java.io.File.createTempFile();

To get the sytem properties list, I use:
System.getProperties().store(System.out, "");
> System.getProperties().store(System.out, "");

Nice!!! :-D
>>
To get the sytem properties list, I use:
System.getProperties().store(System.out, "");
>>

Why?
.... that is because I didn't see in your comment that you are doing the same using the list() method :))
You should call list, otherwise you're creating Writers quite redundantly.
actually - this:
System.getProperties().store(System.out, "");
can be used only for test purposes
Avatar of Baha

ASKER

HI thx for replying to my question!
Actually, i need such a method for being able to get the local temporary directory on any computer using windows.
I use a function that attaches a file that is stored in the first parameter (the path). Here is the function:
attachFile("K:\Project\Report.xml", "file", "Report.xml","multipart/form-data");
So instead of writing this current path, i'd like to use the getTempDir method or the getProperties one in order to get the file which has been stored before in the temporary directory. Maybe if i put as the first parameter :
attachFile(System.getProperties().store(System.out, "Report.xml"), "file", "Report.xml","multipart/form-data");
Maybe this could work? but i don't think so.. well i hope you understood my question. Thx in advance.
As CEHJ said in his first post...

String tempDir = System.getProperty("java.io.tmpdir");
No, with this code

>>
attachFile(System.getProperties().store(System.out, "Report.xml"), "file", "Report.xml","multipart/form-data");
>>

even if System.getProperties().store were appropriate here (it isn't) it's not good code anyway, for the reasons i've already given.

With your function, it should be
attachFile(System.getProperty("java.io.tmpdir"), "file", "Report.xml","multipart/form-data");

to CEHJ:
this is the code of the list method:
    public void list(PrintWriter out) {
     out.println("-- listing properties --");
     Hashtable h = new Hashtable();
     enumerate(h);
     for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
         String key = (String)e.nextElement();
         String val = (String)h.get(key);
         if (val.length() > 40) {
          val = val.substring(0, 37) + "...";
         }
         out.println(key + "=" + val);
     }
    }

as you can see - you will have a problem when a value length exceeds 40 characters.
Also list() method is not appropriate for strings that contain '=', '\n' etc.
>>as you can see - you will have a problem when a value length exceeds 40 characters

Yes, i'm aware of that. The code i posted is not intended to be used in a critical context, as should be clear, since the properties are being dumped to stdout. If any property is truncated, it can be inspected properly by passing the property in question as a parameter to the program.
Baha:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer