Java-从JTextField获取日语并将其保存到文件 [英] Java - Get Japanese from JTextField and save to File

查看:177
本文介绍了Java-从JTextField获取日语并将其保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从JTextField(使用getText()方法)获取日语输入并将其保存到文件中.我相信它确实会从JTextField中获取日文格式,因为我可以将append()String转换为JTextArea,并且它将采用正确的日文格式.

I am trying to get Japanese input from a JTextField (with the getText() method) and saving that to a File. I am confident that it does get Japanese format from the JTextField since I can append() that String to a JTextArea and it will be in the correct Japanese Format.

但是,当我尝试写入文件时,它只会变成乱码!我试图使用以StandardCharsets.UTF_8实例化的OutputStreamWriter,并且尝试使用普通的FileOutputStream,在其中我通过调用String上的getBytes(StandardCharsets.UTF_8)来发送字节.在这两种情况下,生成的文件看起来都更像以下内容:

However, when I try to write to a File it only turns to gibberish! I have tried to use an OutputStreamWriter instantiated with StandardCharsets.UTF_8 and I have tried with a plain FileOutputStream where I send in the bytes from calling getBytes(StandardCharsets.UTF_8) on the String. In both cases the resulting file looks more like the following:

日本語�難������学����ら�日本��む

自然不是我想要的.有谁知道这个问题可能是什么?

Which is not what I want, naturally. Does anyone have any idea what the issue might be?

推荐答案

我很确定您正在使用ISO-8859-1(而不是UTF-8)创建文件. 我还推断您使用的是Eclipse,因为您之前有问题.

I'm pretty sure you are creating the file with ISO-8859-1 instead UTF-8. I'm also inferring you are using Eclipse because your previous questions.

窗口->首选项->常规->工作区:UTF-8

Window -> Preferences -> General -> Workspace : UTF-8

这是我用来测试理论的课程

This is the class i used to test the theory

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class test {
    public static void main(String[] args) throws IOException {
            File fileDir = new File("test.txt");
            String japanese = "路権ち点節ヤトツ限聞ド勇売質タカア";
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir)));
            out.append(japanese);
            System.out.println(japanese);
            out.flush();
            out.close();
    }
}

具有不同设置的输入/输出

OutputFileISO:路権ã¡ç¹ç¯ã¤ããéèãå売質ã¿ã«ã¢

OutputFileUTF8:路権ち点節ヤトツ限聞ド勇売質タカア

OutputFileUTF8: 路権ち点節ヤトツ限聞ド勇売質タカア

这篇关于Java-从JTextField获取日语并将其保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆