Translate Charset : Charset « I18N « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » I18N » CharsetScreenshots 
Translate Charset
    

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class TranslateCharset {
  static public void main(String args[]) throws Exception {
    String inFilename = "inputFileName.txt";
    String inFileCharsetName = "InputFileCharSetName";
    String outFilename = "outputFileName.txt";
    String outFileCharsetName = "OutputFileCharSetName";

    File infile = new File(inFilename);
    File outfile = new File(outFilename);

    RandomAccessFile inraf = new RandomAccessFile(infile, "r");
    RandomAccessFile outraf = new RandomAccessFile(outfile, "rw");

    FileChannel finc = inraf.getChannel();
    FileChannel foutc = outraf.getChannel();

    MappedByteBuffer inmbb = finc.map(FileChannel.MapMode.READ_ONLY, 0(intinfile.length());

    Charset inCharset = Charset.forName(inFileCharsetName);
    Charset outCharset = Charset.forName(outFileCharsetName);

    CharsetDecoder inDecoder = inCharset.newDecoder();
    CharsetEncoder outEncoder = outCharset.newEncoder();

    CharBuffer cb = inDecoder.decode(inmbb);
    ByteBuffer outbb = outEncoder.encode(cb);

    foutc.write(outbb);

    inraf.close();
    outraf.close();
  }
}

           
         
    
    
    
  
Related examples in the same category
1.List the Charset in your system
2.Converting Between Strings (Unicode) and Other Character Set Encodings
3.Charset encoding test
4.encoder and decoder use a supplied ByteBuffer
5.Detect non-ASCII characters in string
6.Displays Charsets and aliasesDisplays Charsets and aliases
7.Encode and DecodeEncode and Decode
8.extends CharsetDecoder to create Base64 Decoder
9.extends Charset to create Hex Charset
10.Get the default charset
11.Charset Toolkit
12.Defines common charsets supported in all Java platforms.
13.How to auto-detect a file's encoding
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.