Linux 拨号vps windows公众号手机端

java如何输出中文字符

lewis 7年前 (2018-10-02) 阅读数 6 #程序编程
文章标签 Java

在Java中,可以使用字符流和字节流来输出中文字符。

  1. 使用字符流输出中文字符:
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
writer.write("你好,世界!");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
  1. 使用字节流输出中文字符:
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
OutputStream outputStream = new FileOutputStream("output.txt");
outputStream.write("你好,世界!".getBytes("UTF-8"));
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

请注意,为了正确地输出中文字符,需要指定正确的字符编码,例如UTF-8。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门