Results 1 to 5 of 5
  1. #1
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default How to read character ?

    Hi,

    I'm haveing a file of huge lines where each line ends with the character

    Reading the file as stream as follows:
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    BufferedReader fileReader = new BufferedReader(new InputStreamReader(inStream));
    String line;
    while ((line = fileReader.readLine()) != null) {
    stream.write(line.getBytes());
    stream.write((byte) '\n');
    }
    return stream.toByteArray();

    I get the each bytes and store the value in the stringbuilder without truncating the above character and converts the stringbuilder to string

    The final converted string doesn't have the above latin character

    Is there is any way to get the string content along with the character ?

    Thanks,
    Kathir

  2. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: How to read character ?

    I don't really understand why you're converting the ascii characters into bytes. you might want to look at your use of the ByteArrayOutputScream aswell. You instantiate it but you never define a stream for it to write too in the constructor or later on.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to read character ?

    If that character is present in your input file it can only be there because of an encoding scheme (such as UTF-8 etc.) You should read that file using that encoding/decoding scheme. Possibly you should also write a file using the same encoding. Read the API documentation for the InputStreamReader class; it explains how to set an encoding scheme.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    j_kathiresan is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default Re: How to read character ?

    When i conver the character to String using UTF-8 encoding i'm getting ?

    Does the character represents the question mark?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to read character ?

    Quote Originally Posted by j_kathiresan View Post
    When i conver the character to String using UTF-8 encoding i'm getting ?

    Does the character represents the question mark?
    No it doesn't; it represents a lower case 'a' with an 'accent grave' on top. So UTF-8 isn't the encoding used for your file. If you're running MS Windows most likely the encoding used is Windows-1252. Both the encoding and the decoding should be the same for simplicity reasons.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to read a Char character ?!
    By Sary in forum New To Java
    Replies: 4
    Last Post: 03-27-2010, 06:01 PM
  2. Read a single character entered
    By mcmb03 in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 10:30 PM
  3. Replies: 8
    Last Post: 09-15-2009, 11:53 AM
  4. Read from a certain character to a certain character
    By blackstormattack in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 11:36 AM
  5. Read character from Image area
    By sundarjothi in forum Advanced Java
    Replies: 2
    Last Post: 01-12-2009, 05:00 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
  •