[英]Taking Japanese or Chinese input from System.in Java
我試圖將日語字符用於我編寫的一個小回顯服務器。 問題是,當我從System.in(通過任何東西,掃描儀, InputStream
命名)中得到字符時,它們總是作為垃圾進來。 我什至嘗試使用
message = new String(bufferedReader.readLine().getBytes("UTF8");
為了嘗試獲取字節以Unicode格式輸入。
當我從服務器print(日文歡迎)打印消息時,它顯示很好,問題僅在接受用戶輸入時存在。
控制台已設置為在Eclipse中使用UTF8。
這是我編寫的一個小型測試程序,以確保它是來自System.in的輸入。
輸入和輸出是
よ
よ
這是代碼
public class TestUnicode {
public static void main(String[] args) throws IOException
{
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in, "UTF8"));
String message = stdIn.readLine();
System.out.println(message);
}
}
public class Client {
public static void main(String[] args) throws IOException
{
Socket serverSocket = null;
try
{
serverSocket = new Socket("192.168.1.127", 3000); //connect to myself at port 3000
}
catch(IOException e)
{
System.out.println(e);
System.exit(1);
}
BufferedReader in = null;
PrintStream out = null;
try //create in and out to write and read from echo
{
in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
out = new PrintStream(serverSocket.getOutputStream(), true);
}
catch(IOException e)
{
serverSocket.close();
System.out.println(e);
System.exit(1);
}
String message = null;
message = in.readLine();
System.out.println(message); //print out the welcome message
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//create a new buffered reader from my input
try
{
while(true)
{
message = bufferedReader.readLine();
out.println(message); //send a line to the server
if(message.equals("quit"))
{
System.out.println(in.readLine());
break;
}
System.out.println(in.readLine()); //get it back and print it
}
System.out.println("Quiting client...");
}
catch(IOException e)
{
in.close();
out.close();
serverSocket.close();
System.out.println(e);
System.exit(1);
}
in.close();
out.close();
serverSocket.close();
}
}
我想您正在使用Windows。
這里的問題是事實,DOS提示符使用與UTF-8完全不同的字符編碼。 如果是日語,則為Shift-JIS,因此嘗試使用UTF-8 InputStream
讀出該信息將不起作用。
幸運的是,有希望。 可以(並且應該)使用System.console()
而不是使用System.in
。 它將返回Console類的一個實例,該實例具有有效的字符編碼轉換。 但是,您必須意識到嘗試從IDE(尤其是Eclipse)中進行調試是行不通的,因為它沒有附加控制台。 哎呀。
更正后的代碼(我確定可以工作,但尚未測試):
public class TestUnicode {
public static void main(String[] args) throws IOException
{
Console console = System.console();
String message = console.readLine();
console.writer().println(message);
}
請注意,您還需要使用Console
打印消息。 為什么? 只是因為您需要同時轉換字符編碼。 DOS提示符仍然保留在舊版編碼中,無法更改它。
創建InputStreamReader時,應指定要使用的字符集:
new InputStreamReader(System.in, "UTF-8")
這也適用於您的套接字流。
如果您不這樣做,那么將使用默認字符集(編碼)。 您還可以通過添加-Dfile.encoding=UTF-8
作為VM參數來更改默認值。
對於您的測試程序,System.out.println也使用默認字符集,因此即使正確讀取字符串也可能使您的字符串混亂。 因此,除非更改默認字符集,否則可以使用類似以下的方法來打印字符串:
final OutputStreamWriter w = new OutputStreamWriter(System.out, "UTF-8");
w.write(message);
w.flush();
我這樣修改了你的課
public class TestUnicode {
/**
* @param args
*/
public static void main(String[] args) {
BufferedReader stdIn = null;
try {
stdIn = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String message = "";
try {
message = stdIn.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println(new String(message.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
並在控制台中運行它,並獲得所需的輸出。
因此,根據您的情況,建議您將字符編碼部分放在BufferedReader和PrintStream中
注意:我嘗試使用IDE運行它並輸出'?' 對於該日語字符,我建議在控制台中運行它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.