Results 1 to 12 of 12

Thread: Write to File

  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Write to File

    *Figured it out, thanks*
    Last edited by sehudson; 06-24-2010 at 12:29 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    how would I avoid the override issue, where each time a .write function is called, the old data is lost
    Can you show an example of this part of your question?

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    I thought that if I use something like
    try{

    FileWriter fstream = new FileWriter("outputfile.txt");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write("Hello Java");
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }

    Is it not true that if I add another "out.write("Hello2 Java");" line underneath the first one, it will just rewrite the whole file, removing the first one. I assumed that I would have to just make one long out.write and add a few html <br> to seperate the lines.

    I cant figure out how to send the JTable over though, since .write pertains to only strings.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Is it not true
    Yes, it is not true.

    if I add another "out.write("Hello2 Java");" line underneath the first one, it will just rewrite the whole file,
    Have you tried it? What happened?

    how to send the JTable over though, since .write pertains to only strings
    What do you want to have in the file? Java has classes that can write many different kinds of data.
    They all end up as bytes when they are in a file.

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    Ok, you're right. It did not erase the previous entry.

    So what I would like to have happen is write to a file in a format like:

    Name: (name variable passed in)
    Date: (get System date)

    JTable with headers preserved

    More Text



    Thats it. Any ideas? I have seen threads where people suggest using ImageIO
    but it seems to suggest that I need to convert the JTable to an image, but I cant figure out how to do that. Also, since ImageIO.write requires a RenderedImage, I would need it in that format if that is the route I go.

    Just not sure how to do it and keep the table displayed as it would be in my program.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Name: (name variable passed in)
    Date: (get System date)
    Those would be two Strings ending with newline chars. No problem.

    JTable with headers preserved
    Can you describe what that would look like in chars or bytes or ? The program is going to have to do some work here to extract what you want to save. And the input program would need compatible code to extract and build a JTable from the file.

    copy over to a word document
    What kind of word document? You'd need a package that writes out that kind of file.

    If the data is saved as an image, you wouldn't be able to read it in again. It would be like a screen print. How useful is that?

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    Ok,

    the JTable data is the results of database queries. So it would be maybe 5 columns, most of which contain regular strings, and some have numbers. (does that help). and I would like to write it out to a normal Microsoft Word file. The end goal is to click on a JButton, have it build the Word File with the 2 strings, JTable, and maybe a few more strings at the end, and print it out.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    I would like to write it out to a normal Microsoft Word file.
    You'll need some third party package to write MS Word files.

  9. #9
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    hmm..Ok

    any Ideas on how to make it work with a Notepad or Wordpad file?
    Maybe I could extract all the data from the table(and headers) and print them out to the file with a tab after each one? Just not sure how to do it.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    If you just want a .txt file, then it will be easier.
    As you suggest, extract the data bit by bit and write it out with tabs between Strings:
    There are several methods to write Strings, I'll use one called writeStr() which is not a real one to show the syntax:
    fileObj.writeStr("First col" + "\t" + "Second column" + "\t" + ...);
    The \t is the tab character. Put it where you want to tab.

  11. #11
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    Ok,

    I'm not following you, i understand the concept, just not completely.

    How would I iterate over the table to find out how many columns there are and how many rows there are? I'm assuming I need to know this to determine how many tabs to do. The table size will vary depending on the query results.

  12. #12
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    I'm guessing it would be something like
    String[] C = {"col1","col"2,"col3"} *varies in size*
    for (int i=0; i<C.length; i++){
    out.write(C[i]+"\t");
    }


    ??

Similar Threads

  1. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  2. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 03:54 PM
  3. Write to a file in a jar
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 06-12-2009, 09:27 PM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. [SOLVED] File Write in File
    By Eku in forum New To Java
    Replies: 4
    Last Post: 10-27-2008, 04:01 AM

Posting Permissions

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