7 Replies Latest reply: Jan 18, 2007 1:48 AM by 807599 RSS

    Reading a unicode file !!!

    807599
      I'm able to display the unicode text in my jsp page. That is through the character reading, but there is a limit in this reading when it exceeds its limit it shows the error. Could i use something else than character reading. Could anyone help me out in this unicode reading. Here is my code
      File ff = new File(tmp,"unicodeMsg.txt");
                      if(ff.canRead()) {
                          FileInputStream in = new FileInputStream(ff);
                          byte len = (byte) ff.length();
                          byte[] ch = new byte[len];                        
                          in.read(ch,0,len);              
                          String strUnicode = new String(ch,"UTF-8");
                          report.setUnicodeValue(strUnicode);
                      } else {
                          report.setUnicodeValue("-");
      It is displaying the unicode character but when i write more than 50 or even more than that then i'm not able to display it. Could anyone tell me what i've to use to read the unicode character.
      in advance thanks....
        • 1. Re: Reading a unicode file !!!
          807599
          Use a BufferedStream or a BufferedReader as you prefer.

          I have no idea what limit you are on about or what any of this has to do with any charset in particular or unicode and I rather think you don't know either.

          So try a buffered in of some sort and then if you are having problems come back and post whatever error message and whatever exact problem you are having.
          • 2. Re: Reading a unicode file !!!
            807599
            Here i'm reading as byte reader or otherwise for
            String strUnicode = new String(ch,"UTF-8");
            i don't have anything else to read as someother array. For first argument i think it takes only character. I'm getting this error.......
            java.lang.NegativeArraySizeException
                 com.myapp.struts.ReportSummaryAction.execute(ReportSummaryAction.java:57)
                 org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
                 org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
                 org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
                 org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
                 javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
                 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
                 org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
            can u provide me code for this....
            thanks.
            • 3. Re: Reading a unicode file !!!
              807599
              This
              byte len = (byte) ff.length();
              byte[] ch = new byte[len];
              is bad news. You are casting a long into a byte so you're losing 7 bytes worth of valuable information.

              This is a little better.
              int len = (int) ff.length();
              byte[] ch = new byte[len];
              • 4. Re: Reading a unicode file !!!
                807599
                i've put ur code it is working for some limit say 50 characters. But when i give more than that it shows the same error. When there is a text file with say 100 characters unicode in it. I want to read those character and display it on jsp page. When the contents of the unicode file more than 100 characters it is giving me the some error.... or sometimes it truncates the other charcters and show the remaining characters...
                • 5. Re: Reading a unicode file !!!
                  807599
                  Use

                  A

                  Reader
                  • 6. Re: Reading a unicode file !!!
                    807599
                    can u provide me a code for this..... still i'm getting error..... it's an abstract class, only method i've to implement..
                    • 7. Re: Reading a unicode file !!!
                      807599
                      //InputStreamReader
                      
                      public InputStreamReader(InputStream in,
                                               String charsetName)
                                        throws UnsupportedEncodingException
                      
                      //Create an InputStreamReader that uses the named charset.