This discussion is archived
4 Replies Latest reply: Jul 20, 2013 2:31 PM by Omariot RSS

Writing to same file

Omariot Newbie
Currently Being Moderated

So I am trying to write the output of two different java class files to one txt file while the program runs.  The file name is determined before the program is ran, through the command prompt as arguments.  How can I get the second class file to edit the same txt file without running into compile errors.

 

For right now I'm just going to send everything that the second file outputs to a message String variable, so that the Main class outputs to the the text file.  I still want to learn how to write to the same text file directly from the second class file.  Don't need the code written just a couple of pointers in the right direction.

 

package Week7.Project3;
import java.io.*;
public class Test{
    public static void main(String[] args) throws IOException{
        int x;
        //create a new file internally called doc.  But externally labelled the user input
        File doc = new File(args[0]);
        if (doc.exists()){
            System.out.println("File exists");
            System.exit(1);
        }
        //creates new output writing method to the doc file
        PrintWriter write = new PrintWriter(doc);
      
        //Creates 10 instances of the Banjo
        write.println("Let's gather 10 Banjos.");
        Banjo instruments[] = new Banjo[10];
        for (x = 0; x < instruments.length; x++){
            instruments[x] = new Banjo();
        }
        //Tunes all 10 Banjos
        write.println("\nTuning the 10 Banjos.");
        for (x = 0; x < instruments.length; x++){
            instruments[x].tunedBanjo();
        }
        //Give even banjos Resonators
        write.println("\nLets give the even Banjos resonators.");
        for (x = 1; x < instruments.length; x+=2){
            instruments[x].setResonator();
        }
        //Starts playing banjos
        write.println("\nLets start playing the Banjos.");
        for (x = 0; x < instruments.length; x++){
            instruments[x].setPlayBanjo();
        }
        //Stops playing the banjos
        write.println("\nLets stop playing the Banjos.");
        for (x = 0; x < instruments.length; x++){
            instruments[x].stopBanjo();
        }
        write.close();
    }
}

 

second file

 

package Week7.Project3;
import java.util.Arrays;
import java.io.*;
public class Banjo{
    private int numberStrings = 5;
    private String banjoName = "Banjo "; //first name of  each banjo
    private String stringNotes[] = {"D", "B", "G", "D", "G"};
    private String arg;
    private boolean tunedStatus; //Is the banjo tuned?
    private boolean playStatus; //Is the banjo Playing?
    private boolean resonator; //Open back or resonator
    private static int banjoNumber = 0; //helps name banjos
  
    public Banjo(){
        tunedStatus = false;
        playStatus = false;
        resonator = false;
        banjoNumber += 1;
        banjoName += banjoNumber;
        write.println(banjoName + " created. \n\t It has " + numberStrings + " strings." +
                "\n\t Notes: " + Arrays.toString(stringNotes) + "\n\t Not tuned with open back.");
    }
  
    public void tunedBanjo(){
        tunedStatus = true;
        write.println(banjoName + " is now tuned");
    }
  
    public void setPlayBanjo(){
        playStatus = true;
        write.println(banjoName + " is now playing");
    }
  
    public void stopBanjo(){
        playStatus = false;
        write.println(banjoName + " has stopped being played.");
    }
  
    public void setResonator(){
        resonator = true;
        write.println(banjoName + " now has a resonator.");
    }
}
  • 1. Re: Writing to same file
    Jörg Explorer
    Currently Being Moderated

    You have to pass the created PrintWriter to the Banjo file, e.g.

    for (x =0; x < instruments.length; x++) {
         instruments[x] = new Banjo(write);
    } 
    

    and then of course modify the Banjo's constructor

    public Banjo(PrintWriter write) 

  • 2. Re: Writing to same file
    Omariot Newbie
    Currently Being Moderated

    Wow, it is as easy as passing variables.

    I had to go one further step and pass that PrintWriter to a local static PrintWriter so that all my methods could use it.

    Thanks for the help.

  • 3. Re: Writing to same file
    Jörg Explorer
    Currently Being Moderated

    Welcome.

     

    >I had to ... pass that PrintWriter to a local static PrintWriter.

    This is only because you have all your code of the Test class in its main method which itself is static.

    If you transfer the code outside of main static is not a must any more.

     

    > ...so that all my methods could use it

    But since this is a requirement, to pass the PrintWriter to a class variable (this.write= write;) is necessary anyhow.

     

    Regards

    J.

  • 4. Re: Writing to same file
    Omariot Newbie
    Currently Being Moderated

    I'm not familiar with the below.

    But since this is a requirement, to pass the PrintWriter to a class variable (this.write= write;) is necessary anyhow.

    I'll do some research into it and see how it works.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points