Results 1 to 2 of 2
  1. #1
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default print toString to .txt file

    Hey I am trying to print an entire toString method + default header to a .txt file . I also want to make the name of the file a variable which changes(A persons name).

    I currently have a GUI made that displays the text in a JTextArea(after calculations are made which are also dynamic). I want to add some text(sort of like a header) to the toString and then concatenate all text from the JTextArea after and print that information to a .txt file using a String which represents that persons name. The printing to the text file would happen when a button is clicked and after a file is written, the information could be changed to write many more new files if need.

    Please help with some suggestions.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    * Create a Writer (probably a FileWriter) based on the desired name. Checking of course that it's a valid filename for your OS.
    * Write the header
    * Write the text content (probably using the JTextComponent method write())
    * Close the Writer

    Notice that there is *no* need to concatenate the header with toString() which might involve much memory usage.

    Most applications this side of Microsoft's hugly popular and profitable BSOD game are dynamic, so it goes without saying that the filename, header and content of the text area will be generated as the program runs (ie are dynamic). Just create the Writer using the name variable.

    You may well find the Basic IO section of Oracle's Tutorial useful.

Similar Threads

  1. using the toString method to print an array
    By Wolfie in forum New To Java
    Replies: 1
    Last Post: 03-06-2011, 10:41 PM
  2. how to pretty print xml into file
    By BigBear in forum XML
    Replies: 2
    Last Post: 02-23-2010, 03:59 PM
  3. Unable to Print more than 1 File
    By satyam000 in forum Networking
    Replies: 1
    Last Post: 11-24-2009, 07:25 AM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  5. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •