0

I am learning the basics of reading from text files. I have code that works fine if everything is in the main method. However, for this exercise I am asked to put the open and close methods into separate methods. The open method takes one argument (the filename) and the close method takes no arguments.

The open method works fine. The close method is my problem.

import java.io.*;
class  EmpInFile
{
    public static void main(String[] args) throws IOException {
        EmpInFile myFile = new EmpInFile() ;
        myFile.openFile("payslip.txt") ;
        myFile.closeFile() ; 
    } // end main

public void openFile(String filename) throws IOException {
    String line ;
    int numLines ;
    // open input file
    FileReader reader = new FileReader(filename) ;
    BufferedReader in = new BufferedReader(reader) ;
    numLines = 0 ;
    // read each line from the file
    line = in.readLine() ; // read first
    while (line != null)
    {
        numLines++ ;
        System.out.println(line) ; // print current
        line = in.readLine() ; // read next line
    }
    System.out.println(numLines + "lines read from file") ;
} // end openFile

public void closeFile() throws IOException {
    in.close() ;
    System.out.println("file closed") ;
    } // end closeFile
} // end class
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158

5 Answers5

4

I think this is bad design. Your openFile() method is doing much more than that - it's reading the complete contents and echoing them to the console (useless, but that's what you're doing).

I don't see what value your close() method is providing. You'd better pass in a File to close. What have you done when you simply wrap the method from java.io.File? At least handle the exception so users don't have to.

I would not recommend using class variables. You can write three methods that are static that will be a lot more useful:

package utils;

public class FileUtils
{
    public static Reader openFile(String fileName) throws IOException
    {
        return new FileReader(new File(fileName)); 
    }

    public static List<String> readFile(String fileName) throws IOException
    {
        List<String> contents = new ArrayList<String>();

        BufferedReader br = null;

        try
        {
            br = new BufferedReader(openFile(fileName));
            while ((String line = br.readLine()) != null)
            {
                contents.add(line);
            }
        }
        finally
        {
            close(br);
        }


        return contents;
    }

    public static void close(Reader r)
    {
        try 
        {
            if (r != null)
            {
                r.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
2

To have openFile and closeFile share some data, you need to put that data as a field in the class.

import java.io.*;
class  EmpInFile
{
   // shared data here.
   BufferedReader in;

   public void openFile() {
     ... set something in "in"
   }

   public void closeFile() {
     ... close "in"
   }
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
1

You need to make in a class-level field.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Your file pointer is not visible in the close method. To have the open and close methods, your FileReader needs to be a member variable.

kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
0

I just want to add something here. It is not an answet but a useful information for you. You do not need to close both BuffuredReader and FileReader. Closing either one of BufferedReader is enough. Previous question was answered here

Community
  • 1
  • 1
gigadot
  • 8,879
  • 7
  • 35
  • 51