简体   繁体   English

Java bufferedreader不读取文件

[英]Java bufferedreader not reading file

I have a method called "add" which takes a string as an argument and uses the bufferedwriter to write it to the file.我有一个名为“add”的方法,它接受一个字符串作为参数,并使用 bufferedwriter 将其写入文件。 Once this is done, the bufferedwriter is flushed.完成此操作后,将刷新缓冲写入器。

In another method "read", I loop through the lines in the file, but the lines are null (hence I cannot print them).在另一种“读取”方法中,我遍历文件中的行,但这些行为空(因此我无法打印它们)。

When I call "read" inside "add", I can print the lines nonetheless.当我在“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
}

This is the full code:这是完整的代码:

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
}

} }

What you should try to do is to create a new instance of BufferedReader/Writer each time you do a read/write operation with the file.您应该尝试做的是在每次对文件进行读/写操作时创建一个BufferedReader/Writer的新实例。 Make sure to flush and close after each use.确保每次使用后冲洗并关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM