java、Android 字符串、json写入文件

来源:互联网 发布:js 数组 splice 删除 编辑:程序博客网 时间:2024/05/22 12:35
 //字符串、json 写入文件    public static void writeStringToFile(String json, String filePath) {        File txt = new File(filePath);        if (!txt.exists()) {            try {                txt.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }        byte[] bytes = json.getBytes(); //新加的        int b = json.length(); //改        FileOutputStream fos = null;        try {            fos = new FileOutputStream(txt);            fos.write(bytes);            fos.flush();            fos.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }
0 0