Results 1 to 3 of 3
- 08-19-2010, 06:26 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
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:
and this is the download codeJava 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);
please help me!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" ); } }
-Armen
- 08-19-2010, 08:37 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
Don't do that; do this instead:
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.Java Code:bos = new BufferedOutputStream( new FileOutputStream(destination) );
kind regards,
Jos
- 08-19-2010, 10:44 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Creating the evaluation function for Minimax
By matzahboy in forum New To JavaReplies: 7Last Post: 11-05-2009, 03:29 PM -
Creating files stopped creating...
By Dieter in forum Advanced JavaReplies: 3Last Post: 09-25-2009, 11:45 PM -
How to download temporary files as well as web page
By baiwen in forum New To JavaReplies: 2Last Post: 03-28-2009, 06:50 AM -
help creating two classes that function together
By sinreaver in forum New To JavaReplies: 6Last Post: 10-01-2008, 03:44 AM -
How to get the temporary deployment folder of ejb from jboss
By ms987654321 in forum Advanced JavaReplies: 1Last Post: 08-22-2008, 12:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks