Writing to the same file

So, I'm writing the output of two different in a txt file java class files, while the program is running.  The name of the file is determined before the program is run through the prompt as arguments.  How can I get the second class file to change the file txt even without compilation errors.

For now, I'll just send everything as the second file outputs a message string variable, so that the main class for the text file.  I still want to learn to write in the same text file directly in the second class file.  Don't need 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.");
    }
}

You have to pass the PrintWriter created to the file of Banjo, for example

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

and then of course change manufacturer the banjo

public Banjo (writing PrintWriter)

Tags: Java

Similar Questions

  • PDF file designed using IREPORT tool. When I saw in the fonts "BOLD" of adobe reader displays properly, but displays of the same file in mozilla plugin from adobe fonts unbold

    I designed a report using IReport tool with a text like "BOLD", that generates the report in PDF format. When I open the file with Adobe Reader IX it opens perfectly.

    but the problem when I open the same file in mozilla Add on "Adobe Reader 11.0.3.37" and in previous versions, is not displayed the "BOLD" font.

    I solved it... the problem is not with mozilla add on... the problem is in jaspersoft...

    in any case thanks for the reply...

  • Firefox asks me if I would record the same file repeatedly

    Firefox repeatedly asks me if I want to save the same file. I delete the downloads.sqlite file. I disabled "don't forget the download history. I've upgraded Firefox 7 to 8 Firefox. Still having the same problem. I did uninstall and reinstall Firefox. I deleted the file from my computer. I still have the same problem. Problem only when Firefox is running.

    I am running Windows Vista Home Premium with Service pack 2 (32 bit).
    

    Suggestions how warns we stop/small window pop up that says "you have chosen to open...". You want to save this file?

    Problem seems to be related to the use of speed dial add-on for FF. Some of the documents stored in the speed dial repeatedly ask you to record files/programs.

  • Same html file seen locally on the laptop seems different from the same file uploaded to the server why?

    I create a file local html with CSS style sheets, etc. I see locally and he looks at the way in which it is intended, when I download and see from a domain to a server, same look of bigger files. This includes images, fonts, etc.. I even tried to reset firefox without result, but since it's the same file, I am just posting locally and online, there should be no reason for these differences. OS is Vista Home Premium, Mozilla Firefox 3.6.13.

    Reset the page zoom on pages that cause problems: view > Zoom > reset (Ctrl + 0 (zero); Cmd + 0 on Mac)

  • My vi crushes always on the same file

    Hello

    My vi filled a table inside a loop, and when it ends then it must save table to a file of lvm. My problem is that it always crash on the same file instead of increment of its name as confirured in the Main.vi "registration".

    simplified "Rempli_Global_Vol_Tot.vi": "."

    Peter,

    the code should look like this:

    Norbert

  • Automatic update Windows maintains the same file.

    Windows Update wants to update the same file KB954430 says there sucsessful it made 12 times but she always wants to do a date put all suggestions?

    Hello

    Control Panel - Windows Updates - right click on and HIDE.

    You can also run these to restore updates.

    How to reset the Windows Update components? -a Mr Fixit
    http://support.Microsoft.com/kb/971058

    Description of the system for Windows Vista, Windows Server 2008, update tool and
    for Windows 7
    http://support.Microsoft.com/kb/947821

    I hope this helps.
    Rob - bicycle - Mark Twain said it is good.

  • Write a string and an integer and a table all in the same file?

    Hello

    I am currently it several different types of values with LabView.

    I have a shot, a few numbers and several paintings.

    Thus, for example, I have a timestamp of the chain, several values of "integer" amplitude of the signal for example, RMS value, frequency and I have several paintings - table of signal, the FFT (PIC and location) values.

    Basically, I'm trying to find a way to write all the values in a single file. I can write all the individual types to separate files (so I can write the RMS, amplitude and frequency to a single file, some of the tables in the other)
    but is it possible to write a string and an integer and a table all in the same file?

    Pointers would be much appreciated,

    Thank you

    Paula

    Your file will be all text... any format in a table of text, to build as a single table, "table chain worksheet", to write to the file.

    (I'm sure this has been on the forums before... a search it would have thrown upward)

  • How to record the numbers and words in the same file

    Hello:

    I did a vi where I record the spectrum and its integration in different positions of a two-dimensional net. I save the information in two spreadsheet with the comand "write to file measure."

    Now, I am recording the parameters initial positions, end X X and space between measurement points. I want a file with two columns that looks like:

    Initial position X 1000

    final position X 2000

    space 100

    But idon't know how to save the words and numbers in the same file.

    As I have to perform several steps I want to automatically choose the name of the file (something like parameters_1, parameters_2...)

    Thank you for your attention

    Hi bitxor.

    You can use all the functions of the WriteTotext file to write strings to a file.

    Then you could set up WriteToMeasurementFile' to add new data to existing files (instead of overwrite or renaming)...

    BTW. It is not a good idea to mix lvm files containing arbitrary spreadsheet data!

  • You name the Windows/system32 directory, such int files: shlwapi (4) (2) .dll? I have a lot of files in system32 that look like this often 3 or versions of the same file?

    You name files in the Windows/system32 directory, such: shlwapi (4) (2) .dll?  I have a lot of files in system32 that look like this often 3 or more versions of the same file?

    Hello

    1. you are trying to move files in and out of Windows\System32?

    By default, windows has not usually (4) or (2) and if there are several files that it can be indicative of a program to move files if you do not have them.

  • Is it possible to have multiple windows of the same file open?

    Hello

    I was wondering if it is possible to have multiple windows of the same file open at the same time in Windows XP?

    Thank you!

    Dave

    You're welcome, Dave. If you click directly on a file, it will be open only once.  That's why I gave an example of starting with work stations and by navigating to the folder (open indirectly).  You can access a the desktop folder in C:\Documents and Settings\\Desktop.
    Boulder computer Maven
    Most Microsoft Valuable Professional

  • I get the same files repeat KB and how to stop it?

    I get the same files repeat KB and how to stop it?

    The KB record number are KB2478663 and KB2518870.

    Mike

    http://answers.Microsoft.com/en-us/Windows/Forum/windows_vista-windows_update/how-to-stop-the-KB-file-from-keeping-repeating-to/21fb21b2-9a13-4261-9c1a-e7ef90a9e6a0

  • multiple paths to the same file come in research

    When I search for a file, I get the multiple as paths:

    C:\Users\Bill\My Documents\... file

    and

    C:\Documents and Settings\Bill\My Documents\ drop...

    The "Documents and Settings" folder contains a shortcut arrow.  There is only one folder for each of the folders in the path starting with 'Bill', that is, they are the same file in two ways.  That is this folder shortcut to, do I need, can I get rid of him and still have my files?  Have no idea how it got there.

    This is the direct download link to the usefulness of the junction box , wait approximately 5 seconds or more for the dialog "Save file" to appear, the name of the file is Junction100.zip (622 KB).  This kind of utilities are sometimes reported as virus by AV software, as long as you know the origin of the file and understand what the utility is supposed to do you will be sure to run the file, what is mentioned on the web site of the author:

    Note that this utility has undergone some fake antivirus alerts, including Norton and McAfee. The Manager of coding point of view there is nothing much that we can do about it, it seems as if some suppliers of software antivirus today write code if the trigger-happy it signals to ANY program that makes the fundamental system changes (which this must do, or it would be of no use!) as potential malware. Especially, the best AV products don't produce any these false alerts.

    Otherwise I have no way of knowing what permissions have been changed on your machine and that I have to post a playfile SubInACL to restore the permissions... believe me, the utility from the link above is much easier to use!

    John

  • background color works do not in the same file

    I have created a variant of background property in my main.qml file and try to access the same file, but cannot. However I can access this variant of property in OTHER qml files. What gives, ideas? Here is my simplified main.qml file.

    The error I get when you declare the background is: 'value of the 'background' property Type mismatch. "Expecting painting and found null"

    However I can access this variant of property in OTHER qml files.

    TabbedPane {
        id: mainTabPage
        property variant appBackground: Color.create("#D2D5D5")
    
        Tab {
            id: tab1
            NavigationPane {
                id: navPane1
                Page {
                    id: page1
                    Container {
                        background: mainTabPage.appBackground // ERROR HERE
                    }
                }
            }
        }
    
        Tab {
            id: tab2
            ....
        }
    }
    

    Never mind. Found the solution on my own. This is a good workaround and less headaches. You set the varaint of proposal for the hex HTML color string.

    And then in your context, just to create the color with this hexadecimal string as a parameter. Here's how

    And then you can use this hexadecimal string ranging from property and the change in the other leaves, as your settings page.

    Hope that helps.

    TabbedPane {
        id: mainTabPage
        property variant appColorBackground: "#D2D5D5"
    
        Tab {
            id: tab1
            NavigationPane {
                id: navPane1
                Page {
                    id: page1
                    Container {
                        background: Color.create(appColorBackground)
                    }
                }
            }
        }
    
        Tab {
            id: tab2
            ....
        }
    }
    
  • Problem with 2 wires to the same file

    I have a processing thread and a thread detachment.  The processing thread generates XML files and places them in a directory under/user/home.  The XML files are not large, generally less than 2 KB.  When the message is complete, it deletes the XML file.  Sometimes, he won't delete them.  If I navigate to the file and try to remove, he says that it is still in use.  I can't understand how it goes because it is not consistent.  My best guess is that the display thread accesses the file before it is closed by the saving thread.  The saving thread does not release the file so that the display thread cannot remove it.  She keeps just the view of the same file.  I have the file register a temp, it writes the data and closes the output stream and then renamed with an extension and quits immediately.  The message thread is set to filter by this extension.  I tried debugging on the device but it is inconsistent and I meet in debugging.  I let it run all night with no problems.  Disconnected this morning and 10 minutes later, I got stuck 1.  The others were very well, it's only 1 file.

    How can I avoid this? Can I release the file between the detachment thread locks?  The API to rename said it empty and closes all open them the flow of input/output and the original file no longer exists.  If I caught in the message thread and renamed it (since there is no additional data to write) which will work?  I worry about zombie save son or other file system problems.  I couldn't find a way to get an exclusive lock on a file.  Maybe I'm going about this all wrong.

    Unsynchronized access to the file, the code that you use to write/delete the file

  • How can we prevent the Explorer of research according to the records of multiple copies of the same file?

    Several slightly different copies of the same file, but subfolders will appear in Windows 7 when you perform a global search for files with specific attributes (e.g., size, date, extension).  If you delete a file, you really are by deleting all the files reported.  What are the steps to perform a search and don't report the real file rather than multiple versions of shadow or shortcut of the file?

    Example:
    Research on all files on the C drive beyond 1 GB, I receive a report in which there are 4 files
    games_file. ISO 7,084,400,688 1/1/2011 12:00 am a C:\Documents and settings\COMPUTER\Documents\Stuff\Apps
    games_file. ISO 7,084,400,688 1/1/2011 12:00 am a C:\Documents and settings\COMPUTER\My Documents\Stuff\Apps
    games_file. ISO 7,084,400,688 1/1/2011 12:00 am by C:\Users\COMPUTER\Documents\Stuff\Apps
    games_file. ISO 7,084,400,688 1/1/2011 12:00 am has C:\Users\COMPUTER\My Documents\Stuff\Apps

    WOW - I can save 21 GB deleting 3 files, right?  Nope, it's all the same physical file.  It should be reported as such.

    Hello

    I suggest to check indexed locations and make sure that duplicate records are not explicitly added. If they do, delete or rebuild the search index and check if the problem is still there

    Change advanced indexing options

    http://Windows.Microsoft.com/en-us/Windows7/change-advanced-indexing-options

    If the problem persists, then create a new user account and verify.

    Create a user account

    http://Windows.Microsoft.com/en-us/Windows7/create-a-user-account

    Create a test file in the same location, and check if the same file is returned multiple times.

    If this isn't the case, then follow the steps mentioned in the link to transfer data from the old user account for the new user account.

    Difficulty of a corrupted user profile

    http://Windows.Microsoft.com/en-us/Windows7/fix-a-corrupted-user-profile

Maybe you are looking for

  • Apple Watch reboot

    My iphon more 6 s and my Apple Watch are both on the last update. Whenever I get a notification of text on my watch, it resets.

  • Qosmio F60 turning is not on - HARD drive problem

    My F60 running stock windows 7 and when I turn it on I get a black screen with the following text: + Check the file system on C: +.+ the type of file system is NTFS.+ Volume name is s3a5802d002 +.+ One of your disks needs to be checked for conisitenc

  • Application of the Dock car

    When I look at the running applications (start |) Menu | Manage Apps | Running tab) I see an application called CarDock running. I do not see this application in the application list and can't access it any way. Does anyone know how to do?

  • Re: Windows 8 Recovery Options

    Tried to refresh for sale computer.  Went through the process for several hours but now will not start.  The error message is inaccessible.boot.device.  Are defined and went to the BIOS (F2) on legacy boot but still nothing, but a loop where it tries

  • noise loud and annoying of the DVD drive to the power of the system on

    Hello, I have a laptop Dell Precision M6800 equipped with the stock of DVD GS40N burner. When turn on/off the system the DVD player makes a noise as if it is trying to load a disc while there is not present in the drive Bay. The noise goes away if I