-2

I'm trying to make two objects that are able to read and write to a text file. The problem happens when I execute the program. The text file gets erased. Why is this happening and how do I fix this?

CODE:

Object to read text files:

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;

    public class Reader {

        public static String fromFile;
        public static FileReader fr;
        public static BufferedReader file;

        public Reader(String fileName) throws IOException, FileNotFoundException {
            fr = new FileReader(fileName);
            file = new BufferedReader(fr);
        }

        public void readFile() throws IOException {
            while((fromFile = file.readLine()) != null) {
                System.out.println(fromFile);
            }
        }

    }

Object to write to text files:

    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;

    public class Writer {

        public static FileWriter fw;
        public static BufferedWriter file;

        public Writer(String fileName) throws IOException, FileNotFoundException {
            fw = new FileWriter(fileName);
            file = new BufferedWriter(fw);
        }

    }

Main method to create Reader and Writer objects and use the Reader to read the text file:

    import java.io.IOException;

    public class Main {

        public static Writer toFile;
        public static Reader fromFile;
        public static String fileName = "test123.txt";

        public static void main(String[] args) throws IOException  {

            toFile = new Writer(fileName);
            fromFile = new Reader(fileName);

            fromFile.readFile();
        }

    }
Reddish
  • 11
  • 4
  • 1
    You seem to be trying to read and write to the same file, which is never a great idea, but because you're not instructing the `Writer` to do otherwise, it's resting the contents of the file - this is what it does by default. Perhaps you should have a read of the [JavaDocs for `FileWriter`](https://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html) and see what options are available for preventing it – MadProgrammer Nov 13 '17 at 21:47
  • The text file will start with something written in it and when the program runs, nothing is printed out to the console. When I go and look back at the text file it's been cleared – Reddish Nov 13 '17 at 21:47
  • @Aominè They mean that they are opening the file in write mode without specifying that they might want to append data to it... – MadProgrammer Nov 13 '17 at 21:47
  • 1
    Why are you opening a writer in the first place? I see no actual writing. – Joe C Nov 13 '17 at 21:48
  • I think it's because you are calling `Writer` before `Reader` and `FileWriter` and `BufferedWriter` overwrite any older data in your file. Take a look [here](https://stackoverflow.com/questions/17244713/using-filewriter-and-bufferedwriter-clearing-file-for-some-reason) for a possible solution. – Vasilis G. Nov 13 '17 at 21:49
  • I want to write a program where a user can enter a name and it will go find the line with that name in it and print the information along with it. – Reddish Nov 13 '17 at 21:49
  • 1
    Try to close the files. fr.close(); fw.close(); May be you're facing some accessing issues otherwise usually it never gets erased. – Suvam Roy Nov 13 '17 at 21:50
  • Setting it to `new FileWriter(fileName, true);` seemed to work. I'll look into the JavaDocs to see what else I can find. – Reddish Nov 13 '17 at 21:52
  • 2
    *"I want to write a program where a user can enter a name and it will go find the line with that name in it and print the information along with it"* - So why do you need the file open in write mode? – MadProgrammer Nov 13 '17 at 21:52
  • They can also enter new data or overwrite old data – Reddish Nov 13 '17 at 21:54

1 Answers1

0

This is just my suggestion.

  import java.io.*;

    public class Main {

        public static void main(String[] args) throws IOException  {
            public static String fileName = "test123.txt";
            Read rd=new Read();
            rd.reader(filename);
        }

    }

public class Read
{ 
        public void reader(String filename)
       {
        FileReader fr=new FileReader(filename);
        BufferedReader bfr=new BufferedWriter (fr);
        String text="";
        String line=reader.readLine();
        while(line!=null)
        {
            text+=line;
            line=reader.readLine();
        } 
        System.out.println(text);

       }
   fr.close();
   bfr.close();
}
Suvam Roy
  • 963
  • 1
  • 8
  • 19