java 获取日语读音,Java-从JTextField获取日语并将其保存到文件

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.

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?

解决方案

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.

Change your workspace settings

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

YedGy.png

Default encoding for all content types

FyKG9.png

TestClass

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

}

}

Input/output with different settings

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值