[java] write to a text file throughout multiple methods.

Discussion in 'Programming' started by killaklown, Feb 1, 2008.

  1. #1
    How can I add stuff to the same text file throughout multiple methods? (cannot use parameters)

    Ie:

    public static String addOne(){
    outputFile.print("add this string\n");
    }
    public static String addTwo(){
    outputFile.print("add this string too\n");
    }
    public void print(){
    FileWriter fwriter = new FileWriter("filename.txt");
    PrintWriter outputFile = new PrintWriter(fwriter);
    }
    Code (markup):

    I know that the above code doesnt work (because the static add___ doesnt know what outputFile is).
     
    killaklown, Feb 1, 2008 IP
  2. Arkserver

    Arkserver Banned

    Messages:
    2,533
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    add "stuff" ?
    you can use append or stringbuffer.
    I could write a demo if you want.
     
    Arkserver, Feb 1, 2008 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    'stuff' as in text...

    like, in addOne method, i want it to add:

    outputFile.println("\n **ERROR01** - Item number must be five characters\n");


    and then later on, ill want it to add

    outputFile.println("\n **ERROR02** - Item number must start with 2 letters\n");

    and then in addTwo method, i want to add:

    outputFile.println("\n **ERROR03** - Item number must be numeric in last 3 positions\n");
     
    killaklown, Feb 1, 2008 IP
  4. Arkserver

    Arkserver Banned

    Messages:
    2,533
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    create a global variable and append it.
     
    Arkserver, Feb 1, 2008 IP