aspose file tools*
The moose likes Beginning Java and the fly likes How a character save in 2 bytes in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How a character save in 2 bytes in Java?" Watch "How a character save in 2 bytes in Java?" New topic
Author

How a character save in 2 bytes in Java?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Streams are for reading and writing binary data (you read or write exactly the bytes as they are stored in for example a file).

Readers and writers are a layer on top of streams; they are used to interpret the bytes as text characters. This interpreting is done using a character encoding. The character encoding defines how the bytes in the file must be interpreted to represent text characters.

Ofcourse you can just use an InputStream to read the bytes in the file. But if you want to treat the file contents as text, you'll need to convert those bytes to text characters somehow. That's what you do with a Reader.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thanks friends!

Then InputStream class and its subclasses and OutputStream class and its subclasses,used to read and write binary data,But If we want to read and write as characters, We need to use Reader and writer class

Right?


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Yes, right!
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635



http://www.iam.ubc.ca/guides/javatut99/i18n/text/stream.html

They read stream as char, But they still need typecast?
Is it possible to write that program so no need to typecast?

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

abalfazl hossein wrote:
They read stream as char, But they still need typecast?
Is it possible to write that program so no need to typecast?



You can check out java.util.Scanner class. It provides methods to read different data types from the input stream. You cannot avoid a cast there because- the value is read as an int and to get the char representation for that int, you need to do a typecast.


Mohamed Sanaulla | My Blog
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


Thank you Sir,

In this code, There is no typecast,Then this is possible use InputStreamReader, Right?

Because when I worked with C, We didn't need to typecast to read character.So it was wonderful for me why do I need typecast in JAVA in order to read characters.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

abalfazl hossein wrote:
Because when I worked with C, We didn't need to typecast to read character.So it was wonderful for me why do I need typecast in JAVA in order to read characters.

Lot of difference between C and Java. Its better if we dont compare both of them There are more better APIs in Java which will help you to read the contents from the file: BufferedReader, Scanner and others.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
For example, write a Swing GUI for your application and display the text in a Swing label. Make sure the label uses a font that contains arabic characters.




How to use swing in this code to show arabic font?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

abalfazl hossein wrote:

How to use swing in this code to show arabic font?


Have you worked on using Swing libraries to create a GUI application? You might want to check the Swing Tutorials to get started.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I wrote a simple program but it does not show text area

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

You need to add the component to some container. Component here would be JTextArea and the Container would be JFrame.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Abalfazl, there is a complete working example in the link I posted earlier. Take a look.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thanks Stephan!
Atul Darne
Ranch Hand

Joined: Jul 05, 2009
Posts: 118

it is complimented in 2's complement, and it is according to the machine configuration.


Regards, Atul.
I came to this world on a Learner's License
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thanks All folks, This is the code that write Farsi,Arabic in output, Without using of JAVA GUI, swing or anything.



abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Are you printing the output to a Windows command prompt window, with System.out.println()? The command prompt window normally cannot handle things like arabic text, because it uses a font that does not contain arabic characters.


Look, In fact I hear statement like "command prompt window" or "JAVA console". But I don't know what relationship between them.


If I want to use Farsi Fonts in command prompt window with UTF-8 encoding,It prints question mark.

Is this because DOS does not support UTF-8?

I search this from and find this:

http://www.coderanch.com/t/515724/java/java/System-out-Arabic-Text

The problem is not your program, it's the console. The console uses a font that doesn't contain glyphs for Arabian characters.


What do you mean by console?
Console and command prompt are same?

I know this is very basic question ,But I prefer to know!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

abalfazl hossein wrote:Look, In fact I hear statement like "command prompt window" or "JAVA console". But I don't know what relationship between them.

The Windows command prompt is the "MS-DOS window", you know, the black window in which you type commands to for example compile and run your Java code. I don't know exactly what is meant by the "Java console", but it is probably the window that some web browsers have, in which messages are logged that an applet can write out by using System.out.println(...). For example, Firefox has an option "Extra / Java Console" to open that window. It's only for applets running in a web page in the browser - normal stand-alone Java applications have nothing to do with it.

abalfazl hossein wrote:If I want to use Farsi Fonts in command prompt window with UTF-8 encoding,It prints question mark.

Is this because DOS does not support UTF-8?

Yes, but it doesn't have anything to do with MS-DOS; since Windows XP, MS-DOS is not a part of Windows anymore. The command prompt window just doesn't support all the characters that exist in the Unicode character set.

abalfazl hossein wrote:
What do you mean by console?
Console and command prompt are same?

Yes, they are the same.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
you know, the black window in which you type commands to for example compile and run your Java code.

I compile and run my code on netbeans, What is realtionship between Windows command prompt and system.out.println()?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I take it you mean by Java command prompt the output window in NetBeans.

When we say console, we mean the textual output of your program. If you run the program in NetBeans, console refers to this output windows. If you run your program from the MSDOS command prompt, console refers to this command prompt.

The MSDOS command prompt uses CP437 encoding, if I'm not mistaken. I believe it uses Lucida Console font.
First of all, because it uses this encoding, it will misinterpret characters you print to it. Secondly, even if it did understand what character you meant to print, I believe Lucida Console does not have graphical glyphs to represent the character, and will print a question mark instead.

As for the NetBeans output box, it will understand what character you print if you use any of the standard encodings in Java, at least, that's my guess. So it should have no problem understanding what character you want if you use UTF-8, or anything like that. You still need to set a font that can represent these characters graphically. Try setting the font of your output window to Gothic MS. There should be an option to do that.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thank you very much dear Stephan,




When I run this code, and Use Farsi font,It prints question mark, But If I use windows-1256, It works very well!

My question is:

In web pages, UTF8 is used and characters is shown well.But When We I use UTF8, Characters does not show well.Why?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Where do you run your program, in NetBeans?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Yes, in Netbeans, On Windows
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Well, the only thing I can think of is that NetBeans doesn't expect UTF-8. In this case, your program works well, NetBeans just fails to recognize your output. I will do some testing and let you know.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I took a look, and it seems to be a known problem. NetBeans handles UTF-8 poorly.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
May you explain more details please?
Thanks in advance!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Well, there's no way in NetBeans to set the way you want the output to interpret information you send to System.out.

I also browsed around and several forums report that NetBeans handles UTF-8 encoded text sent to System.out poorly, it doesn't display them correctly.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


Is it possible to set special font or size of font that prints in output box in this code?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Setting whatever font will be useless if the characters are interpreted incorrectly.

Imagine me asking you if you can draw a house on a piece of paper, and you draw a tree instead. It would be a bit like me asking you to paint it with water paint instead in the hope that you actually paint a house instead of a tree this time around.
 
Don't get me started about those stupid light bulbs.
 
subject: How a character save in 2 bytes in Java?