Java FileOutputStream 将编码设置为 utf-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34269906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:29:14  来源:igfitidea点击:

FileOutputStream set encoding to utf-8

javautf-8

提问by istshrna istshrna

a want to set a file to utf-8

想要将文件设置为 utf-8

the FileOutputStream takes just two parameter

FileOutputStream 只需要两个参数

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();

回答by assylias

FileOutputStream is meant to be used to write binary data. If you want to write text you can use a FileWriter or an OutputStreamWriter.

FileOutputStream 旨在用于写入二进制数据。如果要编写文本,可以使用 FileWriter 或 OutputStreamWriter。

Alternatively you could use one of the methods in the Filesclass, for example:

另外,您可以使用的方法之一Files,例如:

Path p = Paths.get(ismmilaf);
Files.write(p, moarif1.getBytes(StandardCharsets.UTF_8));

回答by Remy Lebeau

You need to use OutputStreamWriterso you can specify an output charset. You can then wrap that in a PrintWriteror BufferedWriterif you need printing semantics:

您需要使用,OutputStreamWriter以便可以指定输出字符集。然后,您可以将其包装在 a PrintWriterorBufferedWriter如果您需要打印语义:

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();

回答by Darshan Mehta

That is because you are using different charsetand those characters do not belong to that, could you try using UTF-8, e.g.:

那是因为您使用的是不同的charset并且这些字符不属于该字符,您可以尝试使用UTF-8,例如:

FileOutputStream fos = new FileOutputStream("G://data//123.txt");
        Writer w = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8));
        String stringa = "L'uomo più forte";
        w.write(stringa);
        w.write("\n");
        w.write("pause");
        w.write("\n");
        w.flush();
        w.close();

回答by JoelBonetR

You can specify it as the third param of PrintStream like this:

您可以将其指定为 PrintStream 的第三个参数,如下所示:

foostream = new FileOutputStream(file, true);
stream  = new PrintStream(foostream, true, "UTF_8");
stream.println("whatever");

or using StandardCharsets.DesiredEncoding (i didn't remember it well but maybe you need to use .toString() on it, like:

或者使用 StandardCharsets.DesiredEncoding (我记不太清楚了,但也许你需要在它上面使用 .toString() ,比如:

stream  = new PrintStream(foostream, true, StandardCharsets.UTF-8.toString());