Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3504

Re: Can't display UTF-8 e

From "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
Subject Re: Can't display UTF-8 e
Message-ID <dMqdnZfX7Zd_G7HVRVnyjgA@bt.com> (permalink)
Newsgroups comp.lang.java.gui
References <f1be58a9-aa85-4900-a781-2ebba6655e76@8g2000hse.googlegroups.com>
Date 2011-04-27 15:45 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
jgricourt@free.fr wrote:
> Your example works for me too ! But unfortunately this is not what I
> am trying to do. I got a text string from a UTF-8 encoded file and
> after reading the file I set a String with the text content in order
> to display in a JLabel.
> 

Your Google-fu is too weak! Exercise it more!

--------------------------- 8< ---------------------------------
class CharacterEncodingTest {

     static final String FILENAME = "foo.txt";

     public static void main(String[] args) {
         try {
             writeMyUtf8File(FILENAME, "|-|-|-|-|-");
             final String text = readMyUtf8File(FILENAME);
             SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                     new CharacterEncodingTest(text);
                 }
             });
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

     static void writeMyUtf8File(String name, String text)
             throws IOException {
         FileOutputStream fos = new FileOutputStream(name);
         Writer osr = new OutputStreamWriter(fos, "UTF-8");
         osr.write(text);
         osr.close();
     }

     static String readMyUtf8File(String name) throws IOException {
         FileInputStream fis = new FileInputStream(FILENAME);
         InputStreamReader isr = new InputStreamReader(fis, "UTF8");
         BufferedReader br = new BufferedReader(isr);
         String text = br.readLine();
         br.close();
         return text;
     }

     CharacterEncodingTest(String text) {
         JPanel p = new JPanel();
         p.add(new JLabel(text));

         JFrame f = new JFrame("Char Encoding Test");
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.add(p);
         f.pack();
         f.setLocationRelativeTo(null);
         f.setVisible(true);
     }
}
--------------------------- 8< ---------------------------------


-- 
RGB

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Can't display UTF-8 e "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:45 +0000
  Re: Can't display UTF-8 e "jgricourt" <jgricourt@THRWHITE.remove-dii-this> - 2011-04-27 15:45 +0000
    Re: Can't display UTF-8 e "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:45 +0000
    Re: Can't display UTF-8 e "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:45 +0000
    Re: Can't display UTF-8 e "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:45 +0000
      Re: Can't display UTF-8 e "jgricourt" <jgricourt@THRWHITE.remove-dii-this> - 2011-04-27 15:45 +0000

csiph-web