Linda
Linda

Reputation: 251

how to read and write in arabic in eclipse

I have written this code in eclipse to get some arabic words and then print them

    public class getString {          
        public static void main(String[] args) throws Exception {  
            PrintStream out = new PrintStream(System.out, true, "UTF-8");
            Reader r = new InputStreamReader(System.in, "UTF-8"); 
            char[] str ;
            str= new char[10];
            r.read(str);
            out.println(str);

    }
    }

but the output is this:

شیرین

شیرین

any help??

thanks in advance for your attention

Upvotes: 3

Views: 11635

Answers (3)

M Abbas
M Abbas

Reputation: 6479

Since you are using windows, then this is how you set encoding as UTF 8 in eclipse:

1- Open Run Dialog > select "your java application" > Common Tab > Encoding > Other > "set it to UTF-8"

enter image description here

2- Open Run Dialog > select "your java application" > Arguments Tab > VM Arguments > Add "-Dfile.encoding=UTF-8"

enter image description here

3- Open Window Menu > General > Workspace > Text file encoding should be set to "UTF-8"

enter image description here

Then you can run your application.

Upvotes: 1

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

I would try this setup:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));

String input = in.readLine();
out.write(input);
out.write("\n");
out.flush();

This is how I used to create IO to handle non-latin characters. Note that this has no auto-flush.

Upvotes: 1

BalusC
BalusC

Reputation: 1108712

Just set workspace encoding to UTF-8 by Window > Preferences > General > Workspace > Text File Encoding. This also affects the encoding of the stdout in Eclipse console.

enter image description here

Then you can also just replace new PrintStream(System.out, true, "UTF-8") by System.out.

Upvotes: 8

Related Questions