Results 1 to 3 of 3
  1. #1
    ArmenBaba is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default Creating Temporary files using the createTempFile function problem.

    Hello Fellow Coders,
    So I'm fairly new to Java but I'm comming from a C# background, however that's not helping much.
    Problem:
    I create a temporary file, and download content from a URL and save it to that file. I use the createTempFile method to create the temp file in a specific folder called "temp", the file is created inside that directory but another copy is created in the java root directory (without my permission) and the content that is downloaded is placed in that copy! I don't understand why?

    I create Temporary file using this statement:
    Java Code:
    //global variable in class
    static File file_buffer; 
    static int PdfFileNum = 1;
    final String temp_directory = System.getProperty("user.dir").concat("\\temp\\");
    ...
    String tempName = "tempPdf" + Integer.toString(PdfFileNum++);
    File newFile = File.createTempFile(tempName, ".pdf", new File(temp_directory) );
    file_buffer = newFile;
    this.download(in_url,file_buffer);
    and this is the download code
    Java Code:
         public void download( String fileUrl, File destination ) throws MalformedURLException,IOException
        {
          if (fileUrl != null && destination != null)
          {
             BufferedInputStream bis = null;
             BufferedOutputStream bos = null;
             try
             {
                URL url = new URL( fileUrl );
                URLConnection urlc = url.openConnection();
    
                bis = new BufferedInputStream( urlc.getInputStream() );
                bos = new BufferedOutputStream( new FileOutputStream(destination.getName()) );
    
                int i;
                while ((i = bis.read()) != -1)
                {
                   bos.write( i );
                }
             }
             finally
             {
                if (bis != null)
                   try
                   {
                      bis.close();
                   }
                   catch (IOException ioe)
                   {
                      ioe.printStackTrace();
                   }
                if (bos != null)
                   try
                   {
                      bos.close();
                   }
                   catch (IOException ioe)
                   {
                      ioe.printStackTrace();
                   }
             }
          }
          else
          {
             System.out.println( "Input not available" );
          }
       }
    please help me!
    -Armen

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ArmenBaba View Post
    Java Code:
    bos = new BufferedOutputStream( new FileOutputStream(destination.getName()) );
    Don't do that; do this instead:

    Java Code:
    bos = new BufferedOutputStream( new FileOutputStream(destination) );
    You have created a temp file named, say, /tmp/tempfile1 and its name is tempfile1. Next you create an output file with the name tempfile1 which has nothing to do with the original file.

    kind regards,

    Jos

  3. #3
    ArmenBaba is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    I love you JosAH.
    I downloaded that code from somewhere without even checking the details. Let that be a lesson to me.
    -Thank you

Similar Threads

  1. Creating the evaluation function for Minimax
    By matzahboy in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 04:29 PM
  2. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-26-2009, 12:45 AM
  3. Replies: 2
    Last Post: 03-28-2009, 07:50 AM
  4. help creating two classes that function together
    By sinreaver in forum New To Java
    Replies: 6
    Last Post: 10-01-2008, 04:44 AM
  5. How to get the temporary deployment folder of ejb from jboss
    By ms987654321 in forum Advanced Java
    Replies: 1
    Last Post: 08-22-2008, 01:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •