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

时间:2019-01-06 22:26:55

标签: java utf-8 java-io

这是我的.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 ,{...“

1 个答案:

答案 0 :(得分:2)

writeUTF不会编写标准的unicode,但会在输出之前添加两个字节的长度信息

如果您有意使用writeUTF,则必须使用readUTF再次读取数据。否则,我建议使用OutputStreamWriter

writeUTF()

  

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


**编辑以澄清OutputStreamWriter

要使用OutputStreamWriter,只需将ObjectOutputStream替换为OutputStreamWriter,然后使用write代替writeUTF

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