1,JDK7前的处理方式:

image.png

  1. public class Text01 {
  2. public static void main(String[] args) {
  3. FileReader fileReader = null;
  4. try {
  5. fileReader = new FileReader("G:\\xxx");
  6. } catch (FileNotFoundException e) {
  7. e.printStackTrace();
  8. }finally {
  9. if (fileReader != null) {
  10. try {
  11. fileReader.close();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. }
  17. }
  18. }

2,JDK7后的处理方式:(推荐)

image.png

  1. public class Text01 {
  2. public static void main(String[] args) {
  3. try (FileWriter fw = new FileWriter("G:\\xxx")) {
  4. fw.write("sss");
  5. } catch (IOException e) {
  6. e.printStackTrace();
  7. }
  8. }
  9. }