How to  open the file writer for next entry after stream is close?

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class TryOut {
     public static void main(String args[]) throws IOException{
          int inputQuestionnAireNum = 0;
          int inputPostCode = 0;
          int inputGender = 0;
          int inputAge = 0;
          int x=0;
          Scanner input = new Scanner(System.in);
          FileWriter fwrite = new FileWriter("tryout.txt", true);
          BufferedWriter out = new BufferedWriter(fwrite);
          boolean invalidNum = false;           
          do{
               System.out.print("1 new entry or 0 print");
               x = Integer.parseInt(input.nextLine());  
               if(x == 1)
                  //Questionnaire Number
                    System.out.print("Enter Questionnaire Number [ ] ");
                    inputQuestionnAireNum = Integer.parseInt(input.nextLine());
                    out.write("NumBER OF Q: "+inputQuestionnAireNum+", ");
                    //Postal code
                    System.out.print("Enter Postal Code [ ] ");     
                    inputPostCode = Integer.parseInt(input.nextLine());
                    out.write("PostCode: "+inputPostCode+", ");
                    //Age
                    System.out.print("Enter Age [ ] ");
                    inputAge = Integer.parseInt(input.nextLine());
                    out.write("Age: "+inputAge+", ");
                    //Gender
                    System.out.print("Enter Gender '1' for Male and '2' for for female [ ] ");
                    inputGender = Integer.parseInt(input.nextLine());
                    out.write("Gender: "+inputGender+", \n");
                    out.close();          
                       } while(x != 0);
     Result:
1 new entry or 0 print1
Enter Questionnaire Number [ ] 2
Enter Postal Code [ ] 1
Enter Age [ ] 3
Enter Gender '1' for Male and '2' for for female [ ] 4
1 new entry or 0 print1
Enter Questionnaire Number [ ] 2
Exception in thread "main" java.io.IOException: Stream closed
     at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:98)
     at java.io.BufferedWriter.write(BufferedWriter.java:203)
     at java.io.Writer.write(Writer.java:126)
     at TryOut.main(TryOut.java:31)
Hi, how can i open the file writer stream again for next entry after it is has been closed? if i do not close(); all my entry will not be recorded to the text file. how can i solve this ?
Edited by: metaroot on Feb 18, 2008 6:39 PM

The short answer to your question: use out=new BufferedWriter(fwrite) inside the do loop as well.The only difference that would make would be to make the problem worse. It is poor practice. Stream stacks should be constructed once for the life of the stream.
What he needs to do is close his stream outside the loop, possibly calling flush() where he presently has close().
The more relevant answer is: Why are you not wrapping the user-friendly PrintWriter class, wrapping it around the BufferedWriter?Possibly because he doesn't like the fact that PrintWriter swallows exceptions? which makes it less user-friendly IMO.
The println and print methods are very useful, and do not need closing between loop iterations.Neither does anything else.
By the way,you say that without a close() the BufferedWriter doesn't output to the file.I don't know: I never used/tried it.Even if it doesn't, it will surely output after you come out of the loop.If you don't want to use a PrintWriter, why don't you investigate this?Posting guesses here isn' t really much help, especially about something you've never used. A BufferedWriter has a buffer and it doesn't write anything until the buffer is full, you flush it, or your close it.

Similar Messages

  • How to open the file in gmail

    How to open the file in gmail. As is keep indicate is not support. Previously I can easily open the excel or words sheets that send to my email but After download the Free Trial for Adobe Pro( Convert PDF file to Microsoft words. )

    More information would be helpful, such as the file format and what the error message is in more detail.

  • HT4796 I have made a file using the Pages app.I mailed it to a friend.When she tried opening the same file on her pc at home , It did not open.Please help me as to how to open the file in the Microsoft word format on her pc.

    I have made a file using the Pages app.I mailed it to a friend.When she tried opening the same file on her pc at home , It did not open.Please help me as to how to open the file in the Microsoft word format on her pc.

    Send it as a PDF.

  • How to open the file types of iOSApp(ipa),EPUBFile(.epub) in windows 7(32 bit)

    How to open the file types of iOSAPP(ipa),EPUBfile(.epub) in windows 7(32 bit)

    iOS apps can be added to an iTunes library and thereafter transferred to an iOS device. They cannot be opened directly on a PC.
    epub files can be added to an iTunes library, and thereafter transferred to an iOS device where they can be read with the iBooks application. Files protected with Apple's DRM cannot be opened directly on a PC, though there are programs such as Kindle and Calibre that will open non-protected epub files.
    tt2

  • How do I change the default program setting when I download a file.  Numbers keeps opening the file intended for Quicken.

    For example; Numbers keeps opening downloaded files intended for Quicken.  I can change the program to open the download, but I can't find a way to make the change be the default.

    In Finder locate a file you're having trouble with, highlight the file and type Command I (I as in Ivan). In the box called Open With choose the app you want to use to open the file with. Right below that  you will see Use this apps to open all documents like this one then click the button right below that calledChange All.

  • Urgent!!how to open the file download dialog box????

    Hi all,
    i'm using the browse button for the user to select the file
    and a submit button, which when cliked should open the file
    download dialog box asking for open or save.
    i searched for so many forums and websites and didn't get the
    solution yet.
    i'm using jsp.
    anyone have the jsp code for the same????
    will b more helpful if u send it to me.
    thanx in advance.

    Hi
    Use the Filedialog class.
    Let your button call a method wich contains the following Java code:
    FileDialog filedialog =
    new FileDialog( Frame1.this, "Open a File", FileDialog.LOAD );
    //witch calls this constructor: public FileDialog(Frame parent, String title, int mode)
    //use Filedialog.SAVE to save a file.
    filedialog.show();
    String filename = filedialog.getFile();//to display the name of the file
    String directoryname = filedialog.getDirectory();// to display the directory name

  • How to start the file server for Agile PLM

    when i try to add one attachment for an Item, i got the following error message:
    The preferred file server http://agileserver:8080/Filemgr/AttachmentServlet for user : xxx is down
    how to startup the file mgr?

    Hi is this looks like a question for Agile A9.
    For Agile A9 or Primavera questions, you can use Oracle Support, Support Communities, Yahoo Groups for Agile API or WRAU.
    This forum is for Agile PLM for Process, which is part of the same Agile family but a different product.
    Thanks.

  • How to open the file in a tree control

    I use a tree control to list all files in a selected directory, I checked the sample with LV (Directory hierarchy in tree control.vi) and want to add a function like following:
    Atfer listing all files, if I click a file listed in the tree control, the computer can open the file automaticly.

    Hi,
    you can use the "Open URL in Default Browser.vi" to open all the files you select in your tree.
    Mike
    Attachments:
    Directory Hierarchy in Tree Control_LV85.vi ‏35 KB

  • Iso - how to open the file?

    Hi all,
    completely newbie to mac. Just recieved a file with .iso extention, wonder how to open and mount it.
    (using tiger)
    thanks
      Mac OS X (10.4.6)  

    None of those options will do what you want. If for some reason the steps in the previous post didn't work, drag the iso into the bar on the left side of the Disk Utility, select it, and then press the Mount button at the top of the window.
    (13501)

  • How to RENAME the file in UNIX-Appl. server, after READING it?

    Hi Experts,
    Following is my requirement,
    What we are wanting here is the functionality added to this program that once the UNIX file is processed, that it can be closed and then renamed on the Application server with the yyyymmdd extension.  For example, the program reads and processes the My_file.csv file today.  Once it's finished, it then closes the file and renames it as My_file20080610.csv.  By doing this, it will prevent the same file from being run the next day in case the new file is not sent. 
    So, pls. let me know that, How to RENAME is file in the UNIX - Appl. server, after reading it? obviuosly I closes it at the end.
    thanq

    Hi Srinivas,
    You can create a My_file20080610.csv. Write the content from My_file.csv into My_file20080610.csv. Then using DELETE DATASET delete the file My_file.csv.
    Thanks
    Romit

  • HOW TO PRINT THE MATTER IN FOR ALL ENTRIES

    HI HOW T  PRINT THE FOR ALL ENTRIES MATTER

    Hi Naresh,
    You can only use FOR ALL ENTRIES IN ...WHERE ...in a SELECT statement.
    SELECT ... FOR ALL ENTRIES IN itab WHERE cond returns the union of the solution sets of all SELECT statements that would result if you wrote a separate statement for each line of the internal table replacing the symbol itab-f with the corresponding value of component f in the WHERE condition.Duplicates are discarded from the result set. If the internal table itab does not contain any entries, the system treats the statement as though there were no WHERE cond condition, and selects all records (in the current client).
    For example:
    SELECT * FROM sflight INTO wa_sflight 
    FOR ALL ENTRIES IN ftab 
    WHERE CARRID = ftab-carrid AND 
                 CONNID = ftab-connid AND 
                     fldate = '20010228'. 
    This condition, return all entries of the sflight.
    When using FOR ALL ENTRIES the number of matching records is restricted to the number of records in the internal table. If the number of records in the database tables is too large then join would cause overheads in performance. Additionally a JOIN bypasses the table buffering.
    Thanks,
    Reward If Helpful.

  • How to open the file when i get it using Datasocket?

    I get a file using datasocket,for example ,a bmp file.but How can i
    open it on my computer? It just save in some buffers.
    please explain in details.
    Thanks in advance.

    You need to convert the image file to a 2D array then transport the array via datasocket.
    Take a look at this example program:
    Convert Image to Array
    Ray K
    Applications Engineer

  • How to open the files from the system

    Hai to all
    Here I am doing a project where I have to put backup word files on the browser . I have created an applet with browse , attach and done (Buttons) . By clicking browse I have to get the open dialog box so that i can give the path easily . Any one can help me in that action event.

    If you're calling FileDialog from an applet via a browser you will throw a security exception (check the browser's Java console). As mentioned in my previous post unless your applet is digitally signed opening a FileDialog is not possible (however it is possible using appletviewer as that tool considers the applet "trusted").

  • How to open the file in PDF format

    Hello,
    There is a requirement on our page that , on click of a button a procedure will be called. If that procedure returns “S” i.e success , then I need to open a new browser window and also set the url for the window (all programmatically). That url opens a letter in pdf format. If it is error , I should display the error on the page. Please provide any kind of solutions.

    Where is PDF stored after generation? a blob or a location on unix server? or it gives u a byte stream at runtime?
    Accordingly we can guide u with the solution.
    --Mukul                                                                                                                                                                                                                                                                                                                                                                                           

  • How to open the proper port for your xbox 360 on a NVG589 router for optimal online gaming

    This took me a while to figure out on my own, so hopefully this will save someone else the trouble. My Xbox 360 said my setting were moderate.  Step 1: In your internet browser, type this http://192.168.1.254 into your address bar Step 2: Click on the tab at the top that says Firewall  Step 3: Click on the sub tab that says NAT/Gaming Step 4: In the drop down box located under the heading "Application Hosting Entry" Scroll down the option that says "XBox Live 360" and select it Step 5: In the second drop down box under the same heading find your Xbox 360's ip address and select it. If you don't know you Xbox's ip address it can be found on your console Network settings, select your connection, then select configure network, under basic setting will your ip address, remember that,  Step 6: Click Add  Step 7: Restart Xbox, everything should be running smoothly online Step 8: Profit! 

    Hello, carrlouie87!
    Thanks for posting. I know quite a few users will find your post helpful, and I hope we continue to see you around on the forums!
    As always, let us know if you have any questions or concerns!
    -Mariana

Maybe you are looking for