我正在进行套接字编程,我想通过网络发送unicode字符。但字符显示不正确。
问题是从IntelliJ IDEA IDE运行时正常显示了这些字符。但是当我部署JAR或通过命令行运行时,“???”显示而不是unicode字符。
我正在使用Windows 10 64位。
示例文字:“गूड”
源代码:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
@SuppressWarnings("InfiniteLoopStatement")
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World");
VBox vBox = new VBox();
primaryStage.setScene(new Scene(vBox, 300, 275));
primaryStage.show();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
final TextArea receivedTextArea = new TextArea();
final TextArea sendTextArea = new TextArea();
sendTextArea.setPromptText("Enter message to send.");
vBox.getChildren().addAll(receivedTextArea, sendTextArea);
receivedTextArea.setEditable(false);
new Thread(new Runnable() {
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(4444);
Socket socket = serverSocket.accept();
Scanner in = new Scanner(socket.getInputStream());
while (true) {
receivedTextArea.setText(in.nextLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
try {
Socket socket = new Socket("localhost", 4444);
final PrintStream out = new PrintStream(socket.getOutputStream(), true);
sendTextArea.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER) {
out.println(sendTextArea.getText().trim());
sendTextArea.setText("");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
在任何地方(流,字节串转换等)使用"UTF-8"
完成了这项工作。