cancel
Showing results for 
Search instead for 
Did you mean: 

Language problem when reading from a file

Former Member
0 Kudos

Hello,

Hello,

I have a text file which contains data in Hebrew.

I am using these lines in order to read from the file:

File inputFile = new File("C:
test.txt");

BufferedReader input = new BufferedReader (new FileReader (inputFile));

while ((line = input.readLine()) != null)

....

The problem is that when I print what I read I receive "Gibrish" letters which is impossible to read, it looks like the collection of the data doesn't keep the language. Is there any way to solve this? perhaps converting the String in some way?

Roy

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Roy,

this is a typical encoding issue. Here are some hints:

1. Try to read the bytes of the text instead of chars.

In other words use an InputStream instead of a Reader.

The result should be a byte array.

2. Be carefully when convert bytes to chars or String

Don't do just

String s = new String(bytes);

Do something like

String s = new String(bytes, "UTF-8");

Hope that helps a bit

Frank

Former Member
0 Kudos

10X Frank your hints solved my problem!

For some reason I don't have an option to give points right now so I'll do that later...

Message was edited by: Armin Reichert

(assigned points)

Former Member
0 Kudos

Hello Frank,

One more question regarding your first suggestion:

The reason I am using Reader is because I want to use the method readLine. How can I use it when I use InputStream as you suggested?

Roy

Former Member
0 Kudos

Hi Roy,

as I stated in the other thread (the one with the URLConnection) You could use an InputStreamReader to bridge from InputStream to a Reader:


InputStream is = new FileInputStream("anyfile");
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);

the impotant thing is to care for the correct encoding ("UTF-8") in my example.

Best Regards

Frank

Answers (0)