Convert Java Unicode \uXXXX to UTF8

How could i convert Java Unicode \uXXXX to UTF8? I tried the JAVA program as below but went into trouble. Could you help?

Or, is there conversion executable for download that can perform the job?



import java.io.* ;
import java.util.*;

public class Asc2Uni
{
      public static void main( String [] args )
      {
            
            File infile = null ;
            File outfile = null ;
            BufferedReader source = null ;
            FileOutputStream dest = null ;
            String infilename = null ;
            String outfilename = null ;
            try
            {
                  if (args.length<2)
                  {
                        System.err.println( "\nUsage: java Asc2Uni <source file> <destination file>\n" ) ;
                        return ;
                  }
                  infilename = args[0] ;
                  outfilename = args[1] ;
                  infile = new File( infilename ) ;
                  outfile = new File( outfilename ) ;
                  if (!infile.exists())
                  {
                        System.err.println( "Invalid input file" ) ;
                        return ;
                  }
                  else if (!infile.canRead())
                  {
                        System.err.println( "Can't read input file" ) ;
                        return ;
                  }
                  else if (outfile.isDirectory())
                  {
                        outfile = new File( outfile.getParent() + "\\" + infile.getName() ) ;
                        System.out.println( "Output file corrected to '" + outfile.getPath() + "'" ) ;
                  }
                  else if (!outfile.canWrite())
                  {
                        System.err.println( "Can't write output file" ) ;
                        return ;
                  }
                  else if (!infile.canRead())
                  {
                        System.err.println( "Can't read input file" ) ;
                        return ;
                  }
                  try
                  {
                        // The input source file contain the string \u00ea\u00f1
                        source = new BufferedReader( new FileReader( infile ) );
                        dest = new FileOutputStream( outfile ) ;
                        Writer out = new OutputStreamWriter(dest, "UTF8");
                        while( source.ready() ){
                          out.write( source.readLine() + "\r\n"); // Don't work!
                          out.write( "\u00ea\u00f1" + "\r\n"); // Get what I want
                        }
                 out.close();


                  }
                  finally
                  {
                        if (source!=null)
                        {
                              try
                              {
                                    source.close() ;
                              }
                              catch (IOException e)
                              {
                                    System.err.println( e.toString() ) ;
                              }
                        }
                                else if (dest!=null)
                        {
                              try
                              {
                                    dest.close() ;
                              }
                              catch (IOException e)
                              {
                                    System.err.println( e.toString() ) ;
                              }
                        }
                  }
            }
            catch (IOException e)
            {
                  System.err.println( e.toString() ) ;
            }
      }
}
kenchan2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodingExpertsCommented:
try {
        // Convert from Unicode to UTF-8
        String string = "abc\u5639\u563b";
        byte[] utf8 = string.getBytes("UTF-8");
   
        // Convert from UTF-8 to Unicode
        string = new String(utf8, "UTF-8");
    } catch (UnsupportedEncodingException e) {
}

Hope this snippet helps...
-CE
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kenchan2000Author Commented:
// Still don't work!
out.write( new String((source.readLine()).getBytes("UTF-8"), "UTF-8") + "\r\n");

// Works!
out.write( "\u00ea\u00f1" + "\r\n");

//source.readLine() returns "\u00ea\u00f1"
0
kenchan2000Author Commented:
Finally got the solution

native2ascii -reverse -encoding UTF8 input.txt output.txt
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.