How to create a class that takes a file name and can print its contents?

I'm having trouble creating a class that can read and print from a file. It looks like the file name passed to the constructor is not actually assigned to the fileName variable, or maybe I'm doing something wrong with the File and Scanner objects. I really don't know what is wrong or how to fix it. I'm new and just close files in my class, so I probably don't see anything obvious. Thanks for any help anyone can give :)

Here is all my code and instructions for the appointment below.

Appointment:

Write a FileDisplay class with the following methods:

  • constructor: takes a file name as an argument

  • displayHead: this method should only display the first five lines of the contents of files. If the file contains less than five lines, it should display files of all contents.

  • displayContents: this method should display the entire contents of the file whose name was passed to the constructor.

  • displayWithLineNumbers: this method should display the contents of the file whose name was passed to the constructor. Each line must be preceded by a line number followed by a colon. Line numbers should begin with 1.

My code is:

import java.io.*; import java.util.Scanner; public class FileDisplay { // just using little random .txt files to test it private String fileName = "example1.txt"; public FileDisplay(String fileName) throws IOException { this.fileName = fileName; } File file = new File(fileName); Scanner inputFile = new Scanner(file); // displays first 5 lines of file public void displayHead() { for (int x = 0; x < 5 && inputFile.hasNext(); x++) { System.out.println(" " + inputFile.nextLine()); } } //displays whole file public void displayContents() { while (inputFile.hasNext()) { System.out.println(" " + inputFile.nextLine()); } } // displays whole file with line numbers public void displayWithLineNumbers() { while (inputFile.hasNext()) { int x = 1; System.out.println(x + ": " + inputFile.nextLine()); x++; } } @Override public String toString() { return "FileDisplay [someFile=" + fileName + "]"; } } 

I also wrote a driver application to test the health of the class:

 import java.io.*; public class FileDisplayTest { public static void main(String[] args) throws IOException { PrintWriter ex1 = new PrintWriter("example1.txt"); ex1.println("apple"); ex1.println("pear"); ex1.println("grape"); ex1.close(); FileDisplay test = new FileDisplay("example1.txt"); test.displayContents(); System.out.println(test.toString()); } } 
+5
source share
1 answer

Your problem is here:

 File file = new File(fileName); 

This statement is outside your constructor.

Runs until the constructor starts. Therefore, the file object is created with the wrong name (by default!)! (see here for further reading)

The best approach here: create final fields and use the "telescope constructor"; eg:

 private final String fileName; private final Scanner scanner; public FileDisplay() { this("default.txt"); } public FileDisplay(String fileName) { this.fileName = fileName; this.scanner = new Scanner(new File(fileName)); } 

And now the compiler helps to make sure that your fields are initialized exactly once, in the order in which you put it once in your constructor. And you have the opportunity to create a FileDisplay object using some default file name (where, in fact, I would recommend not to do this).

+3
source

Source: https://habr.com/ru/post/1265710/


All Articles