I need to write some files to a temp folder what is the best practice to do this?

I have an action that exports a file or copies a file from some complex input/outputstream. The XAS can only process actual files. So when I want to process those files a need to create a temp file. Where should I create these files and are there some problems I can expect when doing this?
1 answers

There are 2 simple ways of creating a temporary file. Example 1, let Java create a temp file. The Java.io.File object has a function to create a Temp file. This is the easiest way of creating temp files. Because this way you don't have to think about the security on your temp folder or removing the files when your done. Just call the function: createTempFile the first param is the filename, the second param is the extension you can give both parameters any value that you would like.

Example2 , create a temp file yourself. In the WEB-INF folder there is always a folder that is called tmp. If you need to create a file and you don't want to use the first example this should be the only folder you will use to write to files. Call the function "getTempPath" from the Configuration class, this will return the path from your temp file as specified in your application.conf file. Create your own file in this folder, do with it what you want and remove it when you are done with it.

Personally I always use example2, I like to have control of how and when the file is created, used and removed. This way I can also easily debug my action, I only have to comment the last line (tempfile.delete() ) and I can see the file.

The disadvantage of the second example is that you have to think of removing the file yourself and you have to make sure that the file doesn't exist.

Here are both examples:

public Boolean executeAction() throws Exception

     * Example 1,  Let Java create and delete the temp file 
    //Let Java create the temp file somewhere
    File tempFile1 = java.io.File.createTempFile("SomeFileName", "xls");
    FileWriter writer1 = new java.io.FileWriter( tempFile1 );
    writer1.append( "Some tekst" );

    FileInputStream tempStream1 = new java.io.FileInputStream( tempFile1 );
    Core.storeFileDocumentContent(this.getContext(), this.InputFileDocument1, tempStream1);

     * Example 2,  handle the temp file yourself
    // Get the conf Dont forget the slash behind the absolute path of the temp folder
    String pathToTempFolder = com.mendix.core.conf.Configuration.getTempPath().getAbsolutePath() + "/";

    //Assuming this file doesn't exist
    File tempFile2 = new java.io.File( pathToTempFolder + "SomeFileName.xls" );
    //if a file with the same name could exists you should check that and rename the file   (a good workaround for that problem is using the GUID in the filename) 

    FileWriter writer2 = new java.io.FileWriter( tempFile2 );
    writer2.append( "Some tekst" );

    FileInputStream tempStream2 = new java.io.FileInputStream( tempFile2 );
    Core.storeFileDocumentContent(this.getContext(), this.InputFileDocument2, tempStream2);

    //And when you create a file in de Temp folder from the XAS don't forget to delete the folder

    return true;