Java bufferedreader 未读取文件

Java bufferedreader not reading file

我有一个名为“add”的方法,它接受一个字符串作为参数并使用 bufferedwriter 将其写入文件。完成后,缓冲写入器将被刷新。

在另一种方法“读取”中,我遍历了文件中的行,但是这些行是空的(因此我无法打印它们)。

当我在“add”中调用“read”时,我仍然可以打印这些行。

public String add(String data) throws IOException{

  this.bufferedWriter.write(data);
  this.bufferedWriter.flush();
//this.read(data);
   logger.info("written " +data);
  return data;
}

public String read(String key) throws IOException{
  logger.info("start reading ...: ");
  String line = this.bufferedReader.readLine();

  while (line!= null) {
    logger.info("loop start reading ...: "+line);
    if(line.split(" ").equals(key)) {
       logger.info("reading line: "+line);
       return line;
    }
    line = this.bufferedReader.readLine();
  }

  return "F";  // indicates the key is not in the storage
}

这是完整代码:

public class FileManager {

Path dataDir;
File f;
FileReader fileReader;
BufferedReader bufferedReader;

FileWriter fileWriter;
BufferedWriter bufferedWriter;

public static Logger logger = Logger.getLogger(FileManager.class.getName());



public FileManager(Path dataDir) throws IOException {
    logger.info("in file manager: ");
    this.dataDir = dataDir;
    
    String dirName = dataDir.toString();

    String fileName = "fifo2.txt";
    File dir = new File (dirName);
    dir.mkdirs();
    f = new File (dir, fileName);
     logger.info("file established at "+f.getAbsolutePath());

    if(!f.exists()){
     logger.info("file not there so create new one ");
      f.createNewFile();
    logger.info("file created!!! ");
    }else{
        logger.info("file already exists");
      System.out.println("File already exists");
    }
    
    logger.info("file stage complete");

    
    this.fileReader = new FileReader(f);
    this.bufferedReader = new BufferedReader(fileReader);
    
    
    this.fileWriter = new FileWriter(f, true);
    this.bufferedWriter = new BufferedWriter(fileWriter);
}

public String add(String data) throws IOException{
    
    this.bufferedWriter.write(data);
    this.bufferedWriter.flush();
    //this.read(data);
     logger.info("written " +data);
    return data;
}

public String read(String key) throws IOException{
    
    logger.info("start reading ...: ");

    
    String line = this.bufferedReader.readLine();
    
    while (line!= null) {
        logger.info("loop start reading ...: "+line);
       if(line.split(" ").equals(key)) {
           logger.info("reading line: "+line);
           return line;
       }
       line = this.bufferedReader.readLine();
    }
    
    return "F";  // indicates the key is not in the storage
}

public String delete(String key) throws IOException{
    
    logger.info("Entering deletion in file storage");

    
    String line;

    while ((line = this.bufferedReader.readLine()) != null) {
       if(line.split(" ").equals(key)) {
           line = "DELETED";
           logger.info("del_reading line: "+line);
           bufferedWriter.write(line);
       }
       line = this.bufferedReader.readLine();
    }
    
    return "F";  // indicates the key to be deleted is not in the storage
}

}

您应该尝试做的是在每次对文件执行 read/write 操作时创建一个 BufferedReader/Writer 的新实例。确保每次使用后冲洗并关闭。