1 2 Previous Next 19 Replies Latest reply: Jan 11, 2010 12:15 AM by 793415 Go to original post RSS
      • 15. Re: Display Arabic text
        843810
        I have mailed you the example for reading Arabic text from file
        • 16. Re: Display Arabic text
          843810
          I have mailed you the code for reading Arabic text from file.
          • 17. Re: Display Arabic text
            793415
            faiz.india wrote:
            I have mailed you the code for reading Arabic text from file.
            This is a discussion forum, rather than a help-desk, so why don't you participate in the community by posting your solution to the thread?

            Edit 1:
            posing -> posting

            Edited by: AndrewThompson64 on Jan 11, 2010 3:36 PM
            • 18. Re: Display Arabic text
              843810
              I can definitely do that, please find that source code for the reading the arabic text from file.
              import java.awt.Color;
              import java.awt.ComponentOrientation;
              import java.io.BufferedReader;
              import java.io.FileInputStream;
              import java.io.IOException;
              import java.io.InputStreamReader;
              import java.io.Reader;
              
              
              public class ReadText extends javax.swing.JFrame {
              
                  /** Creates new form ReadText */
                  public ReadText() {
                      initComponents();
                  }
              
                  /** This method is called from within the constructor to
                   * initialize the form.
                   * WARNING: Do NOT modify this code. The content of this method is
                   * always regenerated by the Form Editor.
                   */
                  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
                  private void initComponents() {
              
                      jLabel1 = new javax.swing.JLabel();
                      txtArabic = new javax.swing.JTextField();
                      jButton1 = new javax.swing.JButton();
                      jLabel2 = new javax.swing.JLabel();
                      txtHex = new javax.swing.JTextField();
              
                      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              
                      jLabel1.setText("Text");
              
                      txtArabic.setFont(new java.awt.Font("Lucida Sans", 0, 10));
              
                      jButton1.setText("Read");
                      jButton1.addActionListener(new java.awt.event.ActionListener() {
              
                          public void actionPerformed(java.awt.event.ActionEvent evt) {
                              jButton1ActionPerformed(evt);
                          }
                      });
              
                      jLabel2.setText("Hex");
              
                      txtHex.setFont(new java.awt.Font("Lucida Sans", 0, 10));
              
                      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                      getContentPane().setLayout(layout);
                      layout.setHorizontalGroup(
                              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(txtArabic, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addGap(143, 143, 143).addComponent(jButton1)).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(txtHex, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
                      layout.setVerticalGroup(
                              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(txtArabic, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(txtHex, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(27, 27, 27)));
              
                      pack();
                  }// </editor-fold>//GEN-END:initComponents
                  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
              
                      //readTextFromFile();
                      final String input = readInput();
                      txtHex.setText(toJAVA(input));
              
                  }//GEN-LAST:event_jButton1ActionPerformed
                  public static String toJAVA(String unicode) {
                      String output = "";
              
                      char[] charArray = unicode.toCharArray();
              
              
                      for (int i = 0; i < charArray.length; ++i) {
                          char a = charArray;

              if ((int) a > 255) {
              output += "\\u" + Integer.toHexString((int) a);
              } else {
              output += a;
              }
              }

              return output;
              }

              /**
              * @param args the command line arguments
              */
              public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {

              public void run() {
              new ReadText().setVisible(true);
              }
              });
              }

              public String readInput() {

              StringBuffer buffer = new StringBuffer();
              try {
              FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "/arabic.txt");

              InputStreamReader isr = new InputStreamReader(fis,
              "UTF8");
              Reader in = new BufferedReader(isr);
              int ch;
              while ((ch = in.read()) > -1) {
              buffer.append((char) ch);
              }
              in.close();

              txtArabic.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
              txtArabic.setBackground(Color.ORANGE);
              txtArabic.setText(buffer.toString());

              return buffer.toString();
              } catch (IOException e) {
              e.printStackTrace();
              return null;
              }
              }
              // Variables declaration - do not modify//GEN-BEGIN:variables
              private javax.swing.JButton jButton1;
              private javax.swing.JLabel jLabel1;
              private javax.swing.JLabel jLabel2;
              private javax.swing.JTextField txtArabic;
              private javax.swing.JTextField txtHex;
              // End of variables declaration//GEN-END:variables
              }
              Before running the code example, make sure that you create an UTF-8 format file named arabic.txt, which would contain some arabic text.
              
              
              Regards,
              Faiz.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
              • 19. Re: Display Arabic text
                793415
                faiz.india wrote:
                I can definitely do that, ...
                Good show. Now people who find this post later (even years later) can benefit from your discoveries.
                1 2 Previous Next