Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Closing streams in finally block

807600Nov 5 2007 — edited Nov 5 2007
I have a program where I'm declaring some streams as part of a try block and then attempting to close them as part of the finally block..... I have a book that does something very similar to this (but I'm thinking it must be a little different because it doesn't actually work for me)... within the finally block, my streams are 'unable to be resolved' - which makes some sense. What is the conventional way of doing this?
public static void main(String[] args) {
		try {
			String source = args[0];
			String target = args[1];
			String line = null;
			String token = null;
			StringTokenizer st = null;
			FileInputStream istream = new FileInputStream(source);
			FileOutputStream ostream = new FileOutputStream(target);
			DataInputStream dis = new DataInputStream(istream);
			DataOutputStream dos = new DataOutputStream(ostream);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(dos));

<Some Code>

		} catch (FileNotFoundException ex) {
<Handle Exception>
                } catch (IOException ex) {
<Handle Exception>
		} finally {

			bw.flush();
			bw.close();
			br.close();
			dis.close();
			dos.close();
			istream.close();
			ostream.close();
		}
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 3 2007
Added on Nov 5 2007
4 comments
986 views