简体   繁体   English

在Java创建的HTML文件中表示日语字母

[英]Representing Japanese letters in a Java created HTML-file

I want to write Japanese letters into a Java created HTML-file. 我想将日文字母写入Java创建的HTML文件中。 For doing so, I created a FileOutputStream creating a .html document at a destination (like Desktop, documents). 为此,我创建了一个FileOutputStream ,在目的地(如桌面,文档)创建.html文档。 I wrote some HTML-Code into my Java Code, so it will translate it properly: 我在我的Java代码中写了一些HTML代码,因此它会正确翻译它:

package main;

import java.io.DataOutputStream;

public class Streamer {
  static String address;
  static String output;

  public Streamer() {}

  static String html_begin = "<!DOCTYPE html><html><body>";
  static String header = "<head><meta lang=\"japanese\" charset=\"unicode\"></head>";
  static String html_end = "</html></body>";

  public static void output(String s, String s2) {
    address = s2;
    output = s;
    setAddress(s2);


    try
    {
      DataOutputStream stream = new DataOutputStream(new java.io.FileOutputStream(address));
      stream.writeBytes(html_begin);
      stream.writeBytes(header);
      stream.writeBytes(output);
      stream.writeBytes(html_end);


      stream.close();
    }
    catch (java.io.IOException localIOException) {}
  }

.....}

Even with declaring the charset as Unicode and the language as Japanese, the created file is showing me some random signs... 即使将字符集声明为Unicode并将语言声明为日语,创建的文件也会向我显示一些随机的符号......

For information: The code directly transforms a given code at a JTextArea ( String ) into the HTML-embedded document. 有关信息:代码直接将JTextAreaString )中的给定代码转换为HTML嵌入文档。 The given address as argument of FileOutputStream is just the destination the created file shall appear. 作为FileOutputStream参数的给定地址只是创建文件出现的目标。

The method setAdress is just the setting method of these address. 方法setAdress只是这些地址的设置方法。

Do not use DataOutputStream to write text files. 不要使用DataOutputStream来编写文本文件。 Use a Writer to write text files. 使用Writer编写文本文件。 Better yet, use a PrintWriter . 更好的是,使用PrintWriter

To write text files with Japanese characters, you need the file to be encoded in a character set that supports Japanese characters, eg UTF-8 or UTF-16. 要编写带有日文字符的文本文件,您需要将文件编码为支持日文字符的字符集,例如UTF-8或UTF-16。

Also, you should use try-with-resources . 此外,您应该使用try-with-resources

try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(Paths.get(address), StandardCharsets.UTF_16))) {
    out.println(html_begin);
    out.println(header);
    out.println(output);
    out.println(html_end);
} catch (IOException e) {
    e.printStackTrace();
}

Pre-Java 7 you would write: 你会写的前Java 7:

try (PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(address), "UTF-16")))) {
    out.println(html_begin);
    out.println(header);
    out.println(output);
    out.println(html_end);
} catch (IOException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM