Writing data to a txt file file using java classes

I am trying to understand why my code is not writing the output to the textfile as I expect it to work. My program takes a filename as a command line argument, and prints some text to the file as well as the screen. It is a bit more complicated since it uses classes and objects to demonstrate how objects work. Can anyone help decipher why it is not writing to the file? Here's my code:-

public class Mamoonp3test {

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

        //Create array of 10 guitar (Mamoonp3) objects
        final int NUMBER_OF_INSTANCES = 10;
        Mamoonp3[] objectNames = new Mamoonp3[NUMBER_OF_INSTANCES];

        try 
        {       
            String fileName = new String(args[0]);

            for(int i=0; i<NUMBER_OF_INSTANCES; i++) {
                objectNames[i] = new Mamoonp3(FileName);
                System.out.println("This is guitar number: " + i);
                objectNames[i].tuneGuitar();
                objectNames[i].playGuitar();
                objectNames[i].displayAcronym();
                objectNames[i].stopGuitar();
                System.out.println("---------------------------");
                }
        }
        catch (Exception e)
        {
            System.out.println("please provide an input file");
            System.out.println("Usage: java Mamoonp3test filename.txt");
        }
    }

}

import java.io.*;

public class Mamoonp3 {

    final int NUMBER_OF_STRINGS = 6;
    char[] stringNames = {'E','A','D','G','B','E'};
    int[] stringNumbers = {6,5,4,3,2,1};
    String[] stringPitch = {"Sixth","Fifth","Fourth","Third","Second","First"};

    boolean isTuned;
    boolean isPlaying;
    String stringAcronym = new String("Even After Dinner Giant Boys Eat");

    //create a PrintWriter for output
    PrintWriter output;

    public Mamoonp3(String fileName) throws Exception{
        isTuned = false;
        isPlaying = false;
        // create target file
        File targetFile = new File(fileName);
        //create a PrintWriter for output
        output = new PrintWriter(targetFile);
    }

    public void tuneGuitar() {
        System.out.println("The guitar is now tuned.");
        for (int i=0; i<NUMBER_OF_STRINGS; i++) {
            System.out.println(stringNames[i] + " is string number " + stringNumbers[i] + " and ranked " + stringPitch[i] + " in pitch");
            output.print(stringNames[i] + " is string number " + stringNumbers[i] + " and ranked " + stringPitch[i] + " in pitch");
            output.close();
        }
    }

    public void playGuitar() {
        System.out.println("The guitar is now playing.");
        output.print("The guitar is now playing.");
        output.close();
    }

    public void stopGuitar() {
        System.out.println("The guitar is now stoped.");
        output.print("The guitar is now stoped.");
        output.close();
    }

    public void displayAcronym() {
        System.out.println("Always remember your string names!");
        System.out.println("Heres a reminder: " + stringAcronym);
        output.print("Always remember your string names!");
        output.print("Heres a reminder: " + stringAcronym);
        output.close();
    }

}

Answers


You're setting the File of an object that you then do nothing with, that you're not writing with,

Mamoonp3 newObject = new Mamoonp3(fileName);

... and not setting the File in objects that you try to write with. Check which constructors you are using: every Manoop3 object created in the for loop. To see that this is so, check which constructors you're using

I suggest that you change your approach entirely.

  • Get all file input and output out of your Mamoonp3 class.
  • Instead, that class should concern itself with representing the state of the musical instrument, and nothing else.
  • Give the class a decent toString() override method.
  • I & O should go elsewhere in a separate class of its own.
  • Give your I&O class a method that allows you to pass Mamoonp3 objects into it so that they can be written.
  • As an aside, you almost never would use new String(anything). Just use args[0].
  • Always close your PrintWriter when you are done writing. This is likely causing your error.

Edit

Possibly another way to solve this:

  • Create a PrintWriter object in the main method.
  • Give your Manoop3 class a PrintWriter field and a constructor that takes this PrintWriter and sets its field with it.
  • Write with the PrintWriter in Manoop3, but don't close it.
  • Then close the PrintWriter in the main method when all Manoop3 objects have completed their use of it.

Need Your Help

Download QT static build on windows

windows qt static download mingw

I have been trying for months to get QT 5.2.X to compile statically for MinGW on Windows, none of the online guides work. Is there a website that has pre-built the libraries for MinGW QT 5.2.x that...

$jQuery Tree from json data ---> .live is killing me

json treeview jquery tree

I'm trying to build a tree from json data, this data are loaded on demand from php files. My problem is that i can't find a way to get to lvl 3 ;). Here is my code: