Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

java - FileOutputStream set encoding to utf-8

a want to set a file to utf-8

the FileOutputStream takes just two parameter

my code is

 PrintWriter kitaba1 = null;

    try {
       kitaba1 = new PrintWriter(new FileOutputStream(new File(ismmilaf), true ));

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }

    //kitaba1.println(moarif1);
    kitaba1.close();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use OutputStreamWriter so you can specify an output charset. You can then wrap that in a PrintWriter or BufferedWriter if you need printing semantics:

PrintWriter kitaba1 = null;

try {
   kitaba1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(ismmilaf), true), StandardCharsets.UTF_8));

} catch (FileNotFoundException ex) {
    ex.printStackTrace();
}

//kitaba1.println(moarif1);
kitaba1.close();

BufferedWriter kitaba1 = null;

try {
   kitaba1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(ismmilaf), true), StandardCharsets.UTF_8));

} catch (FileNotFoundException ex) {
    ex.printStackTrace();
}

//kitaba1.write(moarif1);
//kitaba1.newLine()
kitaba1.close();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...