在finally语句中关闭作家
import java.io.*; import java.util.List;//nowjava.com import java.util.ArrayList; public class ListOfNumbersTest { public static void main(String[] args) { ListOfNumbers list = new ListOfNumbers(); list.writeList(); } } class ListOfNumbers { private List<Integer> list; private static final int SIZE = 10; public ListOfNumbers() { list = new ArrayList<Integer>(SIZE);//来 自 时代Java for (int i = 0; i < SIZE; i++) list.add(new Integer(i)); } public void writeList() { PrintWriter out = null; try { System.out.println("Entering try statement"); out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) out.println("Value at: " + i + " = " + list.get(i)); } catch (IndexOutOfBoundsException e) { System.err.println("Caught IndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } } } }