如何创建一个接受文件名并可以打印其内容的类?

Squ*_*lts 5 java printing constructor file

我在编写一个可以从文件中读取和打印的类时遇到问题.看起来传递给构造函数的文件名实际上并没有分配给fileName变量,或者我可能在File和Scanner对象上做错了.我真的不知道什么是错的或如何解决它.我是初学者,只是在班上使用文件,所以我可能会遗漏一些明显的东西.感谢任何人的帮助:)

这是我的所有代码和下面的分配说明.

任务是:

使用以下方法编写名为FileDisplay的类:

  • 构造函数:接受文件名作为参数

  • displayHead:此方法应仅显示文件内容的前五行.如果文件包含少于五行,则应显示文件的全部内容.

  • displayContents:此方法应显示文件的全部内容,其名称已传递给构造函数.

  • displayWithLineNumbers:此方法应显示文件的内容,其名称已传递给构造函数.每行之前应该有一个行号,后面跟一个冒号.行号应从1开始.

我的代码:

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 + "]";
    }

}
Run Code Online (Sandbox Code Playgroud)

我还写了一个驱动程序应用程序来测试该类是否正常工作:

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());        
    }
}
Run Code Online (Sandbox Code Playgroud)

Gho*_*ica 3

你的问题在这里:

File file = new File(fileName);
Run Code Online (Sandbox Code Playgroud)

该语句位于构造函数之外

它在构造函数启动之前执行。因此,文件对象是使用错误的(您的默认值!)名称创建的!(请参阅此处以进一步阅读)

这里更好的方法是:将您的字段设为Final,并使用“构造函数伸缩”;像这样:

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));
}
Run Code Online (Sandbox Code Playgroud)

现在,编译器可以帮助您确保您的字段按照您在构造函数中放置一次的顺序准确初始化一次。并且您有机会使用某些“默认”文件名创建 FileDisplay 对象(实际上:我建议不要这样做)。