读取,写入,删除json文件中的特定文本

时间:2018-05-03 12:36:04

标签: android json

我目前正在开发一个读取,写入,更新,删除json file.i的应用程序,它有4个json文件,即data.json(存储名称,地址,区域,引脚),image.json(存储图像名称) ,location.json(存储latitute,经度)和final.json合并上面的json files.final.json将组合来自3个文件的所有数据来制作单个记录。在一个活动中我需要读取名称,日期,在final.json中存储在不同单选按钮中的所有记录的时间。我无法只读取存储在temp.json中的记录的名称,日期,时间,因为temp.json包含latitute,经度,图像名称,名称,地址,引脚,区域,日期,时间。请帮我做以下操作。我还需要删除temp.json文件中的特定记录。

这是我用来从temp.json读取文本的代码。如何从文件中读取名称,日期,时间。我能够读取所有参数 我的temp.json看起来像

{Record:["latitute":"22.456","longitude":"88.56","image_name":"xyz.jpg","name":"abc","address":"xx","area":""22","pin":"99","date":"03/05/2018" ,"time":"18:08:22"]}      {Record:["latitute":"22.456","longitude":"88.56","image_name":"xyz.jpg","name":"abc","address":"xx","area":""22","pin":"99","date":"03/05/2018" ,"time":"18:08:22" ]}


String root = Environment.getExternalStorageDirectory().toString(); //get access to directory path
            File myDir = new File(root + "/GeoPark");//create folder in internal storage
            myDir.mkdirs();// make directory
            File file = new File(myDir, FILENAME);//making a new file in the folder

            if(file.exists())   // check if file exist
            {
                //Read text from file
                StringBuilder text = new StringBuilder();

                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String line;

                    while ((line = br.readLine()) != null) {
                        text.append(line);
                    }
                }
                catch (IOException e) {
                    //You'll need to add proper error handling here
                }
                //Set the text
                String x=text.toString();
                String z=x.replace("{","").replace("date:","").replace("time:","").replace("Record:","").replace("[","").replace("latitude:","").replace("longitude:","").replace("name:","").replace("address:","").replace("pin:","").replace("area:","").replace("image:","").replace("\"","").replace("]","").replace("}","");
                String[] y=z.split(",");

                //rb1.setText(y[3].toString()+","+y[7].toString()+","+y[8].toString()+""+"");
                // rb2.setText(y[11].toString()+","+y[15].toString()+","+y[16].toString());
                //rb3.setText(y[19].toString()+","+y[23].toString()+","+y[24].toString());
                //rb4.setText(y[27].toString()+","+y[31].toString()+","+y[32].toString());


            }
            else
            {
                rb1.setText("Sorry file doesn't exist!!");
            }

///////合并代码////

 static class CopyFileContent {

    public static void main(String[] args) {
        JSONObject  jsonObj2 = new JSONObject();
        try {

            // Here we convert  Object to JSON

            jsonObj2.put("date",a.toString());jsonObj2.put("time",c.toString());// Set the first name/pair
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        String root = Environment.getExternalStorageDirectory().toString(); //get access to directory path
        File myDir = new File(root + "/GeoPark");//create folder in internal storage
        myDir.mkdirs();// make directory
        File destFile = new File(myDir, FILENAME11);//making a new file in the folder
    /* Source file, from which content will be copied */
        File sourceFile1 = new File(myDir,FILENAME12);
        File sourceFile2 = new File(myDir,FILENAME13);
        File sourceFile3 = new File(myDir,FILENAME14);

    /* destination file, where the content to be pasted */
        // File destFile = new File(FILENAME);

    /* if file not exist then create one */
        if (!destFile.exists()) {
            try {
                destFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

       InputStream input1 = null;
        InputStream input2 = null;
        InputStream input3 = null;
        OutputStream output = null;
        InputStream input4=null;

        try {

        /* FileInputStream to read streams */
            input1 = new FileInputStream(sourceFile1);
            input2 = new FileInputStream(sourceFile2);
            input3 = new FileInputStream(sourceFile3);

        /* FileOutputStream to write streams */
            output = new FileOutputStream(destFile,true);

            byte[] buf = new byte[1024];
            int bytesRead;
            output.write("{Record:[".getBytes());
            while ((bytesRead = input1.read(buf)) > 0) {
                output.write(buf, 1, bytesRead);

                RandomAccessFile f=new RandomAccessFile(destFile,"rw");
                long length=f.length()-2;
                f.setLength(length);
                length=f.length();
                f.close();
                output.write(",".getBytes());

            }

            while ((bytesRead = input2.read(buf)) > 0) {
                output.write(buf, 1, bytesRead);
                RandomAccessFile f=new RandomAccessFile(destFile,"rw");
                long length=f.length()-2;
                f.setLength(length);
                length=f.length();
                f.close();
                output.write(",".getBytes());

            }

            while ((bytesRead = input3.read(buf)) > 0) {
                output.write(buf, 1, bytesRead);
                RandomAccessFile f=new RandomAccessFile(destFile,"rw");
                long length=f.length()-2;
                f.setLength(length);
                length=f.length();
                f.close();
                output.write(",".getBytes());
                output.write(jsonObj2.toString().getBytes());

                output.write("]}".getBytes());
                output.write("\r\n".getBytes());
                output.write("\r\n".getBytes());
            }





        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        finally {
            try {

                if (null != input1) {
                    input1.close();
                }

                if (null != input2) {
                    input2.close();
                }

                if (null != input3) {
                    input3.close();
                }

                if (null != output) {
                    output.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

0 个答案:

没有答案