繁体   English   中英

ObjectOutputStream.writeUTF 在开始时写入损坏的字符

[英]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

写UTF()

将两个字节的长度信息写入输出流,然后是字符串 s 中每个字符的修改后的 UTF-8 表示。 如果 s 为 null,则抛出 NullPointerException。 字符串 s 中的每个字符都被转换为一组一个、两个或三个字节,具体取决于字符的值。


** 编辑以澄清OutputStreamWriter

要使用OutputStreamWriter只需将ObjectOutputStream替换为OutputStreamWriter并使用write而不是writeUTF

您可能会发现这个小教程很有帮助:jenkov.com 上的 Java IO:OutputStreamWriter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM