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
}

}

6
  • How are you instantiating BufferedReader and BufferedWriter? Commented Nov 8, 2021 at 10:07
  • In a constructor Commented Nov 8, 2021 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 minimal reproducible example and enhance your question accordingl.y Commented Nov 8, 2021 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 Commented Nov 8, 2021 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 stackoverflow.com/questions/14470557/… for more details and other ideas. Commented Nov 8, 2021 at 10:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This works upon creating new instances of the filereader and filewriter. This means the data is printed side by side when calling bufferedWriter
If this answer has solved your problem, please feel free to upvote and accept it.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.