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

前端之家收集整理的这篇文章主要介绍了java – 如何创建一个接受文件名并可以打印其内容的类?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

任务是:

Write a class named FileDisplay with the following methods:

  • constructor: accepts file name as argument

  • displayHead: This method should display only the first five lines of the file’s contents. If the file contains less than five lines,it should display the file’s entire contents.

  • displayContents: This method should display the entire contents of the file,the name of which was passed to the constructor.

  • displayWithLineNumbers: This method should display the contents of the file,the name of which was passed to the constructor. Each line should be preceded with a line number followed by a colon. The line numbering should start at 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 + "]";
    }

}

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

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());        
    }
}
最佳答案
你的问题在这里:

File file = new File(fileName);

该语句不在构造函数之外.

它在构造函数启动之前执行.因此,文件对象是使用错误的(默认!)名称创建的! (详见here)

这里更好的方法是:让你的领域最终,并使用“构造函数伸缩”;像这样:

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));
}

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

原文链接:https://www.f2er.com/java/437385.html

猜你在找的Java相关文章