Java: Writing Text to a File from Multiple Methods in a Class WITHOUT a main() Method
<p>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 <code>main()</code> method, unlike other answers of this type question have used.</p>

<p>So... A quick outline of what my program is currently doing:
My program has one class (with the <code>main()</code> 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 <code>main()</code> method needs to write the string passed to them to THE SAME file.</p>

<p>Why am I having trouble? I can easily write text to a file from ONE method in the class without the <code>main()</code> with <code>FileWriter</code>, but in order to have all of my other methods to write to the same file, I would need to make <code>FileWriter</code> 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.</p>

<p>My current class without the <code>main()</code> method:</p>

<pre><code>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("&lt;QSO_DATE:" + datez.length() + "&gt;" + datez); &lt;&lt;----NULL POINTER EXCEPTION
    }

    static void storeFreq(String freqz) throws IOException {

        writer.write("&lt;FREQ:" + freqz.length() + "&gt;" + 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) {

    }
}
</code></pre>

<p>Each of these methods needs to write the <code>String</code> passed to them to the SAME file.</p>

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

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