0

I have a method called "add" which takes a string as an argument and uses the bufferedwriter to write it to the file. 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.

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
}

}

Mohamed Yehia
  • 53
  • 1
  • 8
  • How are you instantiating `BufferedReader` and `BufferedWriter`? – Haroldo_OK Nov 08 '21 at 10:07
  • In a constructor – Mohamed Yehia Nov 08 '21 at 10:07
  • 2
    Dont **tell** us what your code is doing, **show** us. If all your assumption about the code you wrote were true, you wouldn't be here asking for help. So please see [mcve] and enhance your question accordingl.y – GhostCat Nov 08 '21 at 10:10
  • most likely the issue is `if(line.split(" ").equals(key)) {` what is the `key` - the array returned from split will never match key – PrasadU Nov 08 '21 at 10:11
  • But note: buffered reader/writer aren't a good fit when you really want to MODIFY the same file and read it ... in the same JVM. See https://stackoverflow.com/questions/14470557/read-and-writing-to-a-file-simultaneously-in-java for more details and other ideas. – GhostCat Nov 08 '21 at 10:11
  • @PrasadU, you are right. This is another mistake. However, I do not enter the while loop's body regardless ... – Mohamed Yehia Nov 08 '21 at 10:15

1 Answers1

1

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. Make sure to flush and close after each use.

Arthur Klezovich
  • 2,595
  • 1
  • 13
  • 17
  • This works upon creating new instances of the filereader and filewriter. This means the data is printed side by side when calling bufferedWriter – Mohamed Yehia Nov 08 '21 at 11:26
  • If this answer has solved your problem, please feel free to upvote and accept it. – Arthur Klezovich Nov 08 '21 at 11:27
  • I was hoping for something that would perhaps allow me to have a separate line per written item, but this seems to be the best I could get :) So thank you Arthur! – Mohamed Yehia Nov 08 '21 at 11:38