Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unicode Problem  RSS feed

 
Srinivas Ivaturi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!