Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ObjectOutputStream.writeUTF在开始时写入损坏的字符_Java_Utf 8_Java Io - Fatal编程技术网

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

Java ObjectOutputStream.writeUTF在开始时写入损坏的字符,java,utf-8,java-io,Java,Utf 8,Java Io,这是我的.json文件: {"Usuarios":[{"password":"admin","apellido":"Admin","correo":"Adminadmin.com","direccion":"Admin","telefono":"Admin","nombre":"Admin","username":"admin"}]} (我试着在评论中将我的代码从西班牙语翻译成英语,writeUTF不写标准的unicode,但在输出前加上两个字节的长度信息 如果您有意使用writeUTF,则必

这是我的.json文件:

{"Usuarios":[{"password":"admin","apellido":"Admin","correo":"Adminadmin.com","direccion":"Admin","telefono":"Admin","nombre":"Admin","username":"admin"}]}

(我试着在评论中将我的代码从西班牙语翻译成英语,
writeUTF
不写标准的unicode,但在输出前加上两个字节的长度信息

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

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


**编辑以澄清
OutputStreamWriter

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


您可能会发现这个小教程的帮助信息:

ObjectOutputStream不用于编写文本或JSON。writeUTF方法不会编写真正的UTF-8文本。请改用。您好&欢迎使用StackOverflow。如果您还没有,请在帮助中心查看。要使这个问题成为一个很好的问题,如果输出有趣,您可以尝试一下任何一个字符串,或者只有你尝试过的JSON,一个没有所有JSON的工作都会偏离真正的问题(错误的输出流写入器被选中)。请考虑重新处理你的问题,让其他有类似问题的人更快地学习/找到他们的解决方案:如果你有问题或需要帮助,我将在为您创建的聊天室中呆上一段时间:另请参见:如何在代码中使用OutputStreamWriter?我是学生,我对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);
    }
{"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"}]}
    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));
 }