I'm trying to read text from a file, but the reader just won't close despite me using what I think is the right code for it. I've been using combinations of different codes on the internet to try and solve the problem, so the code that has been tabbed out have been my previous attempts:
import greenfoot.*; import java.io.*; import java.util.logging.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.FileInputStream; import java.io.InputStreamReader;// (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class reader here. * * @author (your name) * @version (a version number or a date) */ public class reader extends Actor { public void act() { try { //If the constructor throws an exception, the finally block will NOT execute FileInputStream fstream = new FileInputStream("test.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); try { String line = null; while ( (line = br.readLine()) != null ) { System.out.println (line); } } finally { //no need to check for null //any exceptions thrown here will be caught by //the outer catch block in.close(); } } catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } /* try { FileInputStream in = new FileInputStream("test.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while((strLine = br.readLine()) != null) { System.out.println(strLine); } } finally { in.close(); } catch(Exception e){ System.out.println(e); } } */ /*try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("test.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } */ }