Windows temp directory details (Java)

后端 未结 7 2236
一生所求
一生所求 2020-12-20 15:12

I\'m writing a program that needs a generic temp folder. I\'m trying to find details about the Windows Temp folders. There are two paths that I know about -

相关标签:
7条回答
  • 2020-12-20 15:25

    It sounds like you have two programs that need to share temp files and one definitely doesn't want spaces in the path name. Probably the easiest thing to do is:

    1. set the TMP and TEMP variable to a common directory
    2. launch each application (from this modified environment) - which should pick up the temp variable

    So at the command prompt you could do this:

    1. set TMP=c:\mytemp
    2. set TEMP=c:\mytemp
    3. java -cp x;y;z my.application.Entry
    4. run other application (hopefully it also reads the environment for temp/tmp)

    Hope that helps.

    0 讨论(0)
  • 2020-12-20 15:26

    you can try this way

    System.out.println(File.createTempFile("temp-file", "tmp").getParent());
    String property = "java.io.tmpdir";
    String tempDir = System.getProperty(property);
    System.out.println("OS current temporary directory is " + tempDir);
    
    0 讨论(0)
  • 2020-12-20 15:31

    This will give you the path to the windows temp directory in Java.

    File.createTempFile("temp-file", "tmp").getParent()
    
    0 讨论(0)
  • 2020-12-20 15:31

    To answer part of your question - if you're using .NET, you can use the Path.GetTempPath() method of the System.IO namespace to get the location of the temporary directory.

    // Get the path of the temporary directory
    string tempDir = Path.GetTempPath();
    
    // "Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file."
    string tempFile = Path.GetTempFileName();
    
    0 讨论(0)
  • 2020-12-20 15:36

    The %TEMP% environment variable that's defined on my PC (XP SP3) uses the DOS-style abcdef~1 directory names - hence, if you can pull that variable, you should end up with a path without spaces.

    e.g. Start>Run>%TEMP% takes me to C:\DOCUME~1\<user>\LOCALS~1\Temp

    However, if a 'super-user' fiddles around with that variable and points it somewhere else, it's possible that things will fall over. You could look at something like this to retrieve the 8-char-and-no-spaces path.

    0 讨论(0)
  • 2020-12-20 15:50

    use this code

       try {    String s=File.createTempFile("temp-file", "tmp").getParent();
                System.out.println(s);
    
            } catch (IOException ex) {
                Logger. getLogger(Result.class.getName()).log(Level.SEVERE, null, ex);
                }
    
    0 讨论(0)
提交回复
热议问题