how to read and write in arabic in eclipse

2019-07-19 06:26发布

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

3条回答
该账号已被封号
2楼-- · 2019-07-19 06:38

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-19 06:53

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.

查看更多
干净又极端
4楼-- · 2019-07-19 06:59

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.

查看更多
登录 后发表回答