Results 1 to 10 of 10
  1. #1
    montag is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default UTF-8, Java, and text output to an RTF

    I am coding my first commercial application and I'm working through bugs slowly but surely. Here's a problem I've run into:

    I am having the character '▓' and others output to an RTF by this method:

    Java Code:
    FileWriter outputStream = new FileWriter("output.rtf");
    When I open the output file in WordPad it prints a '–ˆ'. I have to use WordPad for this and I also have to have the block character. I'm coding a medical text-based application where the user will eventually look at a printout of a 96 well plate and line it up and the dark boxes represent a positive read and the light boxes represent negative reads. I have to use those because other characters aren't dark enough.

    Any advice? I'm coding in eclipse.

    Also, can you make it output with default font other than Times New?

    I also have noticed in WordPad when I open the output file instead of showing a picture of a colored rtf in the tile bar it's just a regular text picture next to "output - Wordpad" on the top left. This only happens when I open this rtf and not another rtf I create in WordPad.
    Last edited by montag; 05-17-2011 at 06:51 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    What is the hex/binary value of the block character?
    Does Wordpad have any characters that it will display that looks like the block character?

  3. #3
    montag is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    U+2588 █ e2 96 88 FULL BLOCK

    I guess the hex value is e2 96 88 according to Unicode/UTF-8-character table

    WordPad will display this character only if I copy and paste it in or change my outputStream to .doc and then save it as an rtf.
    Last edited by montag; 05-17-2011 at 06:59 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    I copy and pasted the first line of your last post into Wordpad and saved it as Unicode text
    and got the following in the .txt file:
    FFFE 55002B0032003500380038000900
    8825 0900650032002000390036002000
    3800 38000900460055004C004C002000
    4200 4C004F0043004B00
    It looks like the block character was written as 0x8825
    Which is 2588 with reversed bytes.
    Last edited by Norm; 05-17-2011 at 07:17 PM.

  5. #5
    montag is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    What does that mean exactly? It's reversing my unicode characters?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Here's some code that will write a block to a txt file:
    Java Code:
          // UTF-8 rules
          // 1110xxxx	10xxxxxx	10xxxxxx   for 16 bits
          // 11101234	10567890	10123456   the bit positions(1 based)   takes 3 bytes for 1 char
    
          // UTF-8 character U+2588		e2 96 88	FULL BLOCK
          char[] data = {'\u3e00', '\u2588', '<'}; 
          // UTF16 - FEFF 003E 8825 003C       <<< This shows the BLOCK in Wordpad with sqrs on each side
          // UTF16 - FEFF 3E00 8825 003C       <<<< Shows > on left side of BLOCK
          // UTF8 -  3E  E8 A0A5 3C            <<< This shows: >*<
          // UTF8 -  FEFF 3E00 8825 003C       <<<< This shows as > Block and a sqr !!!!
    
          OutputStreamWriter out  = new OutputStreamWriter(new FileOutputStream("BlockUnicode.txt"), "UTF8");
          out.write(data, 0, data.length);
          out.close();

  7. #7
    montag is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks man. That helped a lot, although when I open it and run it in WordPad it still gives me those random characters. I ended up looking around thinking WordPad was the problem and I found AbiWord. I'm going to use AbiWord which is open source and seems a lot better than WordPad. The formatting is the exact same (unlike OpenOffice and MS Word) so I'm a fan, and it prints the blocks. Thanks again! Kudos.

  8. #8
    montag is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Also how do I set the default output font to Courier New at 12 pt?

  9. #9
    montag is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    So I've come back with a new problem. I have virtually completed the backend and made it possible to open a text file with the correct UTF-8 formatting so I can see the black squares. I've since started my first GUI frontend for this. Now I'm trying to have it open in a JTextPane and I am not sure how to specify UTF-8 formatting again so I'm running into the same problem. Any advice? I'm going to keep doing research after lunch to see if I can figure it out. Would I have the FileReader specify UTF-8?

    Thanks again,

    montag

  10. #10
    montag is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    I figured it out. It was all due to the reader and me not specifying UTF8.

    Java Code:
    Reader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
                   
                   //Reader in = new FileReader(file);
                   
                   char[] buff = new char[40000096];
                   int nch;
                   while((nch = in.read(buff, 0, buff.length)) != -1)
                   {
                      //jTextPane0.setDocument(new StyledDocument());
                      jTextPane0.setText(new String(buff, 0, nch));
                   }
    I have yet to fix the buffer size. Thanks again Norm.

Similar Threads

  1. Writing a block of text as it is to output file
    By bikashg in forum New To Java
    Replies: 5
    Last Post: 05-29-2010, 11:38 PM
  2. Output to a text file
    By sfe23 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 10:44 PM
  3. Centering text of output
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 10-02-2008, 10:08 PM
  4. Text Output for a library
    By dream_noir in forum Advanced Java
    Replies: 0
    Last Post: 04-14-2008, 05:31 AM
  5. Simply output the result to a text file.
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:48 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
  •