• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How a character save in 2 bytes in Java?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, right!
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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?

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a simple program but it does not show text area

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to add the component to some container. Component here would be JTextArea and the Container would be JFrame.
 
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abalfazl, there is a complete working example in the link I posted earlier. Take a look.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan!
 
Ranch Hand
Posts: 119
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is complimented in 2's complement, and it is according to the machine configuration.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

https://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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where do you run your program, in NetBeans?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, in Netbeans, On Windows
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took a look, and it seems to be a known problem. NetBeans handles UTF-8 poorly.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May you explain more details please?
Thanks in advance!
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is it possible to set special font or size of font that prints in output box in this code?
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic