This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Unicode Problem

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a file contais this as data. "This is a test\u4321". When I read that file and write it into other file I want the unicode character data to be converted as "This is a test?". I used the following code but not successful.can someone help me in this?

Here is the code snippet.

try
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\in.txt"), "UTF-8"));
PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("c:\\out.txt"), "UTF-8"));
String line;
while ((line = in.readLine()) != null ) {
// do some processing with line, and then:
out.println(line);
}
out.close();
in.close();}
catch(Exception e){
e.printStackTrace();
}
}


The interesting thing is when I assing the line = "This is a test\u4321" and write into file, it is writing properly as "This is a test?"

Thanks
Srinivas Ivaturi
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following uses Rewriter from http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html . Does it give you any ideas?

 
Srinivas Ivaturi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using JDK API Is it possible to get solution for my problem? Because I would expect some Unicode support API being available to us.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivas Ivaturi:
Using JDK API Is it possible to get solution for my problem? Because I would expect some Unicode support API being available to us.



Java has unicode support since ALL string are unicode but you as asking for support in converting \uxxxx to a unicode character. As far as I am aware this is not possible though the standard API though one would expect it to be so because the Java compiler accepts \uxxxx anywhere in the Java source.
 
Sheriff
Posts: 26958
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "\u4321" construct is used in Java only in character and string literals, nowhere else. Only the Java compiler understands that notation. It's a completely wrong idea to use it anywhere else (such as in database columns) and it's completely wrong to expect Java to deal with it in any way except as 6 characters: backslash, letter-u, 4, 3, 2, and 1.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

It is not really true to say that unicode escape characters can only be used in characters or String literals. They can be used anywhere in the Java source code. For example, the following prints 'Hello World'.



James
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic