![](/img/trans.png)
[英]Socket ObjectOutputStream writeUTF() and ObjectInputStream readUTF() not working
[英]ObjectOutputStream.writeUTF writes corrupt characters at the start
這是我的 .json 文件:
{"Usuarios":[{"password":"admin","apellido":"Admin","correo":"Adminadmin.com","direccion":"Admin","telefono":"Admin","nombre":"Admin","username":"admin"}]}
(我盡量在評論中將我的代碼從西班牙語翻譯成英語 <3)
JSON 中寫入的函數是這樣的:
public void agregarUsuario(String nombre, String apellido, String direccion, String telefono, String correo, String username, String password) {
try {
//String jsonString = JsonObject.toString();
JSONObject usuarios = getJSONObjectFromFile("/usuarios.json");
JSONArray listaUsuario = usuarios.getJSONArray("Usuarios");
JSONObject newObject = new JSONObject();
newObject.put("nombre", nombre);
newObject.put("apellido", apellido);
newObject.put("direccion", direccion);
newObject.put("telefono", telefono);
newObject.put("correo", correo);
newObject.put("username",username);
newObject.put("password", password);
listaUsuario.put(newObject);
usuarios.put("Usuarios",listaUsuario);
ObjectOutputStream outputStream = null;
outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Victor\\eclipse-workspace\\Iplane\\assets\\usuarios.json"));
outputStream.writeUTF(usuarios.toString());
outputStream.flush();
outputStream.close();
}catch(JSONException e) {
e.printStackTrace();
}catch(Exception e) {
System.err.println("Error writting json: " + e);
}
因此,如果在我的“創建用戶”JFrame 窗口中,我使用“asdf”作為所有用戶詳細信息中的信息創建一個新用戶,我應該獲得以下 JSON 文件:
{"Usuarios":[{"password":"admin","apellido":"Admin","correo":"Adminadmin.com","direccion":"Admin","telefono":"Admin","nombre":"Admin","username":"admin"},{"password":"asdf","apellido":"asdf","correo":"asdf","direccion":"asdf","telefono":"asdf","nombre":"asdf","username":"asdf"}]}
是的! 那個會發生! 但如果我的 JSON 主對象,我也得到了一些奇怪的 ascii/Unicode 符號。 我無法在這里復制輸出,所以這是我在 imgur 上的輸出: link 。
為什么會出現這個問題? 我怎么能修好呢? 如果有人需要我的 json 文件閱讀器(也許問題出在那里),您可以:
public static InputStream inputStreamFromFile(String path) {
try {
InputStream inputStream = FileHandle.class.getResourceAsStream(path); //charge json in "InputStream"
return inputStream;
}catch(Exception e) {
e.printStackTrace(); //tracer for json exceptions
}
return null;
}
public static String getJsonStringFromFile(String path) {
Scanner scanner;
InputStream in = inputStreamFromFile(path); //obtains the content of the .JSON and saves it in: "in" variable
scanner = new Scanner(in); //new scanner with inputStream "in" info
String json= scanner.useDelimiter("\\Z").next(); //reads .JSON and saves it in string "json"
scanner.close(); //close the scanner
return json; //return json String
}
public static boolean objectExists (JSONObject jsonObject, String key) { //verifies whether an object exist in the json
Object o;
try {
o=jsonObject.get(key);
}catch(Exception e) {
return false;
}
return o!=null;
}
public static JSONObject getJSONObjectFromFile(String path) { //creates a jsonObject from a path
return new JSONObject(getJsonStringFromFile(path));
}
因此,在寫入 JSON 文件后,我無法對它做任何事情,因為使用這個奇怪的符號,我的 json 中出現錯誤:“外部輸入:(這里是符號)期望 [STRING, NUMBER, TRUE, FALSE, {. ..”
writeUTF
不編寫標准的 unicode,而是在輸出前加上兩個字節的長度信息
如果故意使用writeUTF
,則必須再次使用readUTF
讀取數據。 否則我會建議使用OutputStreamWriter
。
將兩個字節的長度信息寫入輸出流,然后是字符串 s 中每個字符的修改后的 UTF-8 表示。 如果 s 為 null,則拋出 NullPointerException。 字符串 s 中的每個字符都被轉換為一組一個、兩個或三個字節,具體取決於字符的值。
** 編輯以澄清OutputStreamWriter
:
要使用OutputStreamWriter
只需將ObjectOutputStream
替換為OutputStreamWriter
並使用write
而不是writeUTF
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.