Hi
I have a simple code snippet here that writes to a file called Student.txt :

import java.io.*;
import java.util.*;

public class Streams {

	public static void main(String[] args) {
		String studentFirstName = "Bob ";
		String studentLastName = " Smith ";
		String finalGrade = "A";
		
		try {
			PrintWriter out = new PrintWriter(new FileOutputStream("Student.txt"));
			out.print(studentFirstName);
			out.println(studentLastName);
			out.print(finalGrade);
							
		}
		catch (IOException e) 	{
			System.out.println(e);
		}
		finally {
			out.close();
		}
	}
}

But I get an error for this bit called 'out cannot be resolved.' ;

finally {
			out.close();
		}

Can anyone explain why?

Recommended Answers

All 8 Replies

Instead of using finally,
After the last out.print add
out.close();
under it.

I tried that, but wouldn't it affect the exception handling because I'm closing the file before it catches the error (i.e. if there is one) ?

No you need to have a finally because even if an exception occurs you will still need to close the PrintWriter. The error happens because you declare PrintWriter inside try {....} and in the finally it is out of scope, it cannot be used outside try because this is where it declared. Try this:

import java.io.*;
import java.util.*;

public class Streams {

	public static void main(String[] args) {
		String studentFirstName = "Bob ";
		String studentLastName = " Smith ";
		String finalGrade = "A";

		PrintWriter out = null;

		try {
			out = new PrintWriter(new FileOutputStream("Student.txt"));
			out.print(studentFirstName);
			out.println(studentLastName);
			out.print(finalGrade);
							
		}
		catch (IOException e) 	{
			System.out.println(e);
		}
		finally {
			if (out!=null) out.close(); //in case you have an exception before  PrintWriter takes value
		}
	}
}

here this worked for me

import java.io.*;
import java.util.*;

public class Streams {

	public static void main(String[] args) {
		String studentFirstName = "Bob ";
		String studentLastName = " Smith ";
		String finalGrade = "A";
		PrintWriter out = null;
		try {
			out = new PrintWriter(new FileOutputStream("Student.txt"));
			out.print(studentFirstName);
			out.println(studentLastName);
			out.print(finalGrade);
							
		}
		catch (IOException e) 	{
			System.out.println(e);
		}
		finally {
			out.close();
		}
	}
}

EDIT:
javaAddict just beat me to it :(

here this worked for me

import java.io.*;
import java.util.*;

public class Streams {

	public static void main(String[] args) {
		String studentFirstName = "Bob ";
		String studentLastName = " Smith ";
		String finalGrade = "A";
		PrintWriter out = null;
		try {
			out = new PrintWriter(new FileOutputStream("Student.txt"));
			out.print(studentFirstName);
			out.println(studentLastName);
			out.print(finalGrade);
							
		}
		catch (IOException e) 	{
			System.out.println(e);
		}
		finally {
			out.close();
		}
	}
}

EDIT:
javaAddict just beat me to it :(

I would suggest you put this in finally:

if (out!=null) out.close();

If the student.txt file does not exist then an exception will be thrown without PrintWriter taking value. So in the finally you will have a NullPointerException.

When i ran the program i did not have the student.txt file. But the program created automatically, so how could i get that error?

Instead of "Student.txt" you could have a String given from input. If you try a String that is not a file then you will get that error.

Even if you don't get that error, you don't know what might happen. I mean since there is a chance that: new PrintWriter( ... ) would throw an Exception, you should make that check in the finally.

Yeah I tried that and it did work. I understand the logic as well. Thanks for explaining :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.