This site requires JavaScript, please enable it in your browser!
Greenfoot back
theprogrammer
theprogrammer wrote ...

2013/3/13

Closing buffered reader

theprogrammer theprogrammer

2013/3/13

#
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());
  }
  }
  */
}       
    
davmac davmac

2013/3/14

#
Could you explain what you mean by "won't close" - how do you know it's not closing?
theprogrammer theprogrammer

2013/3/14

#
Hmms. I guess the code is running over and over, but I what I want it essentially to do is read whatever is in the file, wait for x amount of seconds, and then close it, and that would be the end of the code. Right now, it's just spamming the contents of the file in the console window.
davmac davmac

2013/3/14

#
Well, you've got the code in the act() method of an actor, which is called over and over again (that's what it's there for...). If you moved the code into the actor's constructor (or perhaps the addedToWorld() method) then it would only run once.
theprogrammer theprogrammer

2013/3/14

#
What do you mean by the actor's constructor and the addedToWorld() method? I'm not that strong in regards to codes like that.
davmac davmac

2013/3/15

#
Well, your actor doesn't have an explicit constructor yet, but you could add one:
public reader()
{
    // constructor code goes here
}
This is a 'constructor' because it gets called when the object is created. It is named the same as the class, and doesn't have a return type.
theprogrammer theprogrammer

2013/3/15

#
Thank you davmac! That seems to have worked. I mean, as soon as I have compiled the world, the system console window pops out with the contents of the text file being read. Of course, that isn't how I wanted it to work. I'm figuring that to solve that issue, I would add the object to the world only if a condition has been met? Would that work?
davmac davmac

2013/3/16

#
Yes, that would work. But if the only reason for the actor's existence is to read the file, you might not need it at all. Just use 'if', i.e. instead of:
if (condition) {
     // add the actor to the world
}
do this:
if (condition) {
     // file reading code
}
You need to login to post a reply.