24GHz
24GHz

Reputation: 80

Java: Writing Text to a File from Multiple Methods in a Class WITHOUT a main() Method

I am currently dead in the water with a Java programming problem that seemed somewhat simple at first to do! I am trying to write text to a file from MULTIPLE methods in a class that does NOT contain a main() method, unlike other answers of this type question have used.

So... A quick outline of what my program is currently doing: My program has one class (with the main() method obviously) that reads a text file stored on the disk, and passes sections of the text to certain methods in another class (second file in the project) to simply write the passed text to a text file. Each method in the class without the main() method needs to write the string passed to them to THE SAME file.

Why am I having trouble? I can easily write text to a file from ONE method in the class without the main() with FileWriter, but in order to have all of my other methods to write to the same file, I would need to make FileWriter global. I have tried to make it global, but then when I save text after another method saved text, it just rewrites the file to the latest text written.

My current class without the main() method:

package ADIFTOCSV;

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

public class createADIF {

    static File file;
    static FileWriter fw;
    static BufferedWriter writer;

    static void init() throws IOException {
        file = new File("/Users/Colin/Desktop/Myadif.txt");
        fw = new FileWriter(file.getAbsoluteFile());
        writer = new BufferedWriter(fw);
    }



    static void storeDate(String datez) throws IOException {

        writer.write("<QSO_DATE:" + datez.length() + ">" + datez); <<----NULL POINTER EXCEPTION
    }

    static void storeFreq(String freqz) throws IOException {

        writer.write("<FREQ:" + freqz.length() + ">" + freqz);
        writer.close();
    }

    static void storeMode(String modez) {

    }

    static void storeBand(String bandz) {

    }

    static void storePower(String pwrz) {

    }

    static void storeTime(String timez) {

    }

    static void storeCall(String callz) {

    }

    static void storeRstSent(String rstsentz) {

    }

    static void storeRstRcvd(String rstrcvdz) {

    }

    static void storeComments(String commentsz) {

    }
}

Each of these methods needs to write the String passed to them to the SAME file.

storeDate() is the first method to be called, therefore it writes text to the file first. However, when storeFreq() is called, it's text completely replaces the text written by storeDate(). This is obvious because I am forced to create a new FileWriter object in each method in order to write text. I cannot put FileWriter fw = new FileWriter(file.getAbsoluteFile()); outside the method to make it global; errors arise.

Am I missing something? Help is much appreciated. If any questions arise, or clarification is needed, please just ask!

Upvotes: 0

Views: 3864

Answers (2)

Linora
Linora

Reputation: 10988

I would suggest that you scrap the class with the static methods and instead create a normal "File Write Handler" class which has a constructor where you can pass the File and writer to intialize the file writing classes and let that class handle all the writing to the file such that you can call a method like this:

FileWriteHandler.writer("<FREQ:" + freqz.length() + ">" + freqz);

and soforth for the rest you want printed. And finally call

FileWriteHandler.close();

Would be much cleaner and you could even make an interface for that class such that you can replace the FileWriterHandler with f.ex. a DatabaseWriteHandler or something like that.

Upvotes: 1

Vampire
Vampire

Reputation: 38694

You have to create the writer outside the methods. Just defining the file outside is not enough. If you recreate a writer to the same file in each method, of course it will overwrite. The File instance is just a pointer to the file. The writer is the "actual handle" that you need to reuse.

And be aware that you have to close the writer if you are finished with writing.

Upvotes: 3

Related Questions