Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
How to delete tmp file in the case of abort. Finally block is not in the scope
George Cui
Greenhorn
Posts: 5
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public static void main (String [] args) { try { String num = "100x"; // purposely set to abort\ File file = new File("C://ARA//TESTS/Zxception.txt"); File tmp_file = file.createTempFile("TMP","TEST",file.getParentFile()); int maxLength = Integer.parseInt(num); // abort here tmp_file.deleteOnExit(); } catch (Exception e) { e.printStackTrace(); } finally { // cannot put tmp_file.delete() here since it is out of scope } }
when it abort the tmp file is still in the folder.
How to delete the tmp file in case?
thanks
Paul Clapham
Marshal
Posts: 28193
95
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You just need to declare the variable in a scope which includes both the try-block and the finally-block.
George Cui
Greenhorn
Posts: 5
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks, after adding
File tmp_file = null;
at the top, it works
This is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
why cannot delete file
Checking a string
why cannot delete file
why cannot delete file
Why cannot delete the file?
More...