我想打开一个文件以在构造函数中读取。该文件应该可以用任何方法读取,而无需创建另一个 reader 对象

I want to open a file for reading in the constructor. The file should be readable in any method without creating another reader object

构造函数打开一个文件进行读取。我有一个名为 getNext() 的方法,它 returns 字符串数组中文件的下一行。但是它说找不到已经在构造函数中创建的 BufferedReader 对象 导入 java.io.; 导入 java.util.;

public class SNIDDb
{
  private char delimiter;
  private String name;

  public SNIDDb(String name, char delimiter)
  {
    this.name=name;
    this.delimiter=delimiter;
    try
    {
      FileReader fr= new FileReader(name);
      BufferedReader br= new BufferReader(fr);
    }
    catch(Exception e)
    {
      System.out.print(e.getMessage());
    }
  }

  public Boolean hasNext() throws Exception
  {
    return !(br.readLine()==null); 
  }

  public String[] getNext() throws Exception
  {
    String c_line="";
    c_line=br.readLine();
    String[] n_line=c_line.split(delimiter);
    return n_line;
  }

  public void rewrite() throws Exception
  {
    br.close();
    BufferedWriter writer = new BufferedWriter(new FileWriter(name));
  }

br 是一个局部变量,只能在构造函数内部使用。你应该使它成为一个实例成员变量,如 delimitername.