Results 1 to 6 of 6

Thread: Changing charset on the fly

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Changing charset on the fly

    Hello,
    I have an application currently using a BufferedReader with a default charset. I wish I could edit charset on a fly, while the flow still works. Apparently, this is how my code looks.
    Code:
    InputStream inpustr = new FileInputStream("test.txt") ;
    BufferedReader buff1 = new BufferedReader(
            new InputStreamReader(inpustr, charset)
        );
    buff1.readLine();
    BufferedReader buff2 = new BufferedReader(
            new InputStreamReader(inpustr, charset)
        );
    buff2.readLine();
    I first get out the first line. Then, I get the end of the line containing the character 8.193e (8kb is + / - buffer size used, according to my estimates). For a writer, it's easier and it works, provided flush after each write. And as a bonus, if you could tell me how to do the same thing with the NIO.
    Last edited by Remedy; 21-01-2010 at 10:33 AM.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Changing charset on the fly

    Hello,
    I went through your code, I think you can replace this part of your code
    BufferedReader buff2 = new BufferedReader(
    new InputStreamReader(inpustr, charset)
    By
    Code:
    BufferedReader buff2 = new BufferedReader (
            new InputStreamReader (buff1, charset)
        );
    I think this should work in a more better way? Just check it by replacing this code in your code.

  3. #3
    Join Date
    Dec 2009
    Posts
    192

    Re: Changing charset on the fly

    Hello,
    By my knowledge no class InputStreamReader is used, InputStream only accepts as input, not the Reader class. And BufferedReader is a subclass of Reader class. There is no equivalent to an InputStreamReader reversed, I hope so. I have the distinct impression that I will have to implement my own version of InputStreamReader, to manage change in charset. Am I going in the correct direction. If I am wrong please guide me.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Changing charset on the fly

    Hello,
    I think he means to say this
    BufferedReader buff2 = new BufferedReader( buff1, charset );
    and not this
    BufferedReader buff2 = new BufferedReader (
    new InputStreamReader (buff1, charset)
    );
    I think you should use it, as it is posted above. I think this is just a mistake in posting the content and nothing else.

  5. #5
    Join Date
    Dec 2009
    Posts
    192

    Re: Changing charset on the fly

    Hi,
    It is not possible, only the InputStreamReader plays with charsets. The BufferedReader has two constructors: (Reader) and (Reader, int size). That makes sense, since the Reader return char [] char from [] (except isReadable) InputStream and return the int [] From int []. In java.io.* I have not seen anything approaching your reasoning. That is why I am using the method to make my program work, but unfortunately it is not working successfully yet.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Changing charset on the fly

    Hello,
    Your problem is that BufferedReader buffer fills it whenever it can. You can perhaps use a PushBackInputStream as follows:
    Code:
    byte[] buf = new byte[512]; / / For you to see the size
    String chset1, chset2, string;
    StringBuilder tx;
    PushBackInputStream in;
    int index;
    
    tx = new StringBuilder();
    do {
      tx.append(string);
      in.read(buf, 0, Buffer.size);
      string = new String(buf chset1);
      index = string.indexOf("\ n");
    } while ( Index == -1);
    
    index + +; / / This is to include \ n in the first charset
    tx.append(string.substringing(0, Index));
     
    / / it gets late in the second table charset
    in.unread(buf, index buf.size - Index);
    string = new String(buf, index buf.size - Index, chset2);
    tx.append(string);
     
    / / We read the rest in the second charset
    while(in.available != 0) {
      in.read(buf, 0, Buffer.size);
      string = new String(buf chset2);
      tx.append(string);
    }
    
    System.out.System.out.println(tx.toString());

Similar Threads

  1. Improper charset in cmus on Gentoo
    By fanish-war in forum Windows Software
    Replies: 5
    Last Post: 04-06-2011, 08:33 PM
  2. Changing OEM SLP Key
    By Asis in forum Operating Systems
    Replies: 6
    Last Post: 21-08-2010, 06:22 AM
  3. Changing User Password w/o Changing FingerPrint
    By Patz in forum Hardware Peripherals
    Replies: 1
    Last Post: 09-04-2010, 12:37 AM
  4. What is Charset UTF-8 and ISO-8859-1 in Apache
    By Patinux in forum Operating Systems
    Replies: 3
    Last Post: 08-08-2009, 01:21 PM
  5. Changing color in a GIF
    By Gap meer in forum Customize Desktop
    Replies: 5
    Last Post: 10-03-2009, 10:01 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,052,294.71480 seconds with 16 queries