5 Replies Latest reply: Nov 27, 2006 1:03 PM by 807607 RSS

    create a temporary file..

    807607
      Hi guys,
      i've an easy question for you. I've a jsf application, when i click on a button a method starts that creates a temporary file, run an applet that loads this file and when the applet ends the temporary file has to be deleted.
      What is my problem?
      I've tried to create the temporary file with
      String y="good";
      File w=File.createTempFile("giu",".txt");
                BufferedWriter b;
                b = new BufferedWriter(new FileWriter(w));
      b.write(y);
      i've searched a giu.txt file but it doesn't exists!!!
      Please help me to understand where is my error,thanks
        • 1. Re: create a temporary file..
          JoachimSauer
          According to the JavaDoc of createTempFile it creates the temp file in you temporary directory (which is system dependent). You might want to look there.
          • 2. Re: create a temporary file..
            807607
            To delete a file when the virtual machine exits, use the deleteOnExit() facility

            http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#deleteOnExit()

            When you create a temp file using the method you do, the file is created in the default system temp directory, and the file will be named:
            giuxxxxx.txt
            where the xxxx are randomly generated characters.
            • 3. Re: create a temporary file..
              807607
              thanks guys for your help but it doesn't work!!
              This is my simple method
              public String go()throws IOException{
                        try { 
                             System.out.println("hi");
                           // Create temp file. 
                           File temp = File.createTempFile("aaaaa",.txt ); 
                           System.out.println("hi1");
                           // Write to temp file 
                           BufferedWriter out = new BufferedWriter(new FileWriter(temp)); 
                           System.out.println("hi2");
                           out.write("aString"); 
                           System.out.println("hi3");
                           out.close(); 
                       } catch (IOException e) { 
                            System.out.println("hi100");
                       } 
              
              
              
                        return "greeting";
                   }
              when i run it all try code is correctl executed without throwing exceptions but aaaaa.txt isn't created!!
              How can i do?
              Please help me
              • 4. Re: create a temporary file..
                800322
                when i run it all try code is correctl executed
                without throwing exceptions but aaaaa.txt isn't
                created!!
                Where isn't it created? Where did you look for it?

                And J2EE apps accessing the file system... eeeeew. Why not have the applet request the file from a servlet instead of writing a temp file first? Safer, more reliable, scalable and much less ugly.
                Also, you didn't seem to understand that the file's name will not be aaaaaa.txt.
                • 5. Re: create a temporary file..
                  807607
                  System.out.println(temp.getAbsolutePath());
                  See this path while program is in execution. Introduce a
                  System.in.read() 
                  code line if you need that program waits while you see the file.