cancel
Showing results for 
Search instead for 
Did you mean: 

creating a temporary file in the user computer

Former Member
0 Kudos

Hi everyone,

I want to create a new file using:

File folder = new File(folderString);

when folderString should be a folder where all temporary files of the user are saved (its a dynpage code).

So I found the following code in <b>JavaScript</b>:

var cTemporaryFolder = 2,tmpdir='';

var fso;

fso = new ActiveXObject('Scripting.FileSystemObject');

var fso;

tmpdir = fso.GetSpecialFolder(cTemporaryFolder);

But how do I combine the code with the statement

File folder = new File(folderString);

(I eventually need: folderString = tmpdir - but one variable knows <b>java</b> and the other <b>javascript</b>)

Thanks so much.

I promise to award points...

Ruthie.

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Dear Uma & Amol,

When executing System.getProperty("TEMP")in Dyanpge - I get the temporary directory of the server and I need to get the temporary directory of each user.

(File f = new File("d://TestTemp"); doesn't help because d://TestTemp is a const and I need the temp directory of each user - a dynamic location)

I am not an expert in Java or Dynpage - I am coming from Abap - so I have to explore this field.

Thanks.

Ruthie.

Former Member
0 Kudos

Hi Ruthie,

Try out this code

try{

// Create a temporary file object

File f = new File("d://TestTemp");

File tempFile = File.createTempFile("mail", "temp",f);

System.out.println("Created temporary file: " + tempFile + "\n");

// Delete temp file when program exits

tempFile.deleteOnExit();

// Write to temporary file

BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));

out.write("TEMP FILE: " + tempFile);

out.close();

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Press any key");

br.read();

System.out.println("Temp File will be deleted");

}

catch(IOException e) {

e.printStackTrace();

}

Now, the temp file will be created in d:\TestTemp\ folder. Before pressing any key just check in this folder. You will see the temp file. Pressing any key will exit the program. After this check the d:\TestTemp\ folder, the tempfile will be deleted.

Regards,

Uma

Former Member
0 Kudos

Hi Uma,

The parameter directory of the method createTempFile(String prefix,

String suffix,

File directory)

throws IOException

specifies the directory to be used for creating temp files.

How do I know the user directory where temp files are created ? This directory can varies between different users!

Thanks,

Ruthie.

Former Member
0 Kudos

Ruthie,

Thats where you can make use of System.getProperty("TEMP") which would give you the temp directory for the currently logged on user. For me,on a windows machine it gives a value

TEMP=C:\DOCUME1\MYUSERID\LOCALS1\Temp

TMP=C:\DOCUME1\MYUSERID\LOCALS1\Temp

also on unix based operating system, i am not sure but can check the temp system variable value by executing the command <b>export</b> at the shell prompt.

Former Member
0 Kudos

Hi Ruthie,

There is createTempFile() method with three arguments also.

public static File createTempFile(String prefix,

String suffix,

File directory)

throws IOException

The third argument specifies the directory to be used for creating temp files.

Refer this link to know about deletion of temp files when program exits

http://www.idevelopment.info/data/Programming/java/io/Files/CreateTemporaryFile.java

Regards,

Uma

Former Member
0 Kudos

Hi Amol,

createTempFile method creats a temporary file where the default creation folder is taken as the temporary folder of the server. Also - system variables are giving me information of the server.

But I want to create a temporary file in the user temporary folder.

Do you have any idea ??

Thanks.

Ruthie.

Former Member
0 Kudos

did you try the createTempFile method of File class?

also, if you are on windows then the system variable TEMP points to the temporary files folder so you can try this

File folder = new File(System.getProperty("TEMP"));