将字符串转化为json字符串,创建文件、读取文件

1、工具类


import java.io.*;


/**
 * @Author: jiee
 * @Date: 2020/8/6 9:49
 */
public class JsonUtil {

    /**
     * 从文件中读取数据
     *
     * @param path 文件路径
     * @return 文件内容
     */
    //从给定位置读取Json文件
    public static String readJson(String path) {
        //从指定位置读取文件
        File file = new File(path);

        BufferedReader br = null;

        //返回值,使用StringBuffer
        StringBuffer data = new StringBuffer();
        try {
            br = new BufferedReader(new FileReader(file));
            //每次读取文件的缓存
            String temp = null;
            while ((temp = br.readLine()) != null) {
                data.append(temp);
//                System.out.println(data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
//        System.out.println("读取成功!");
        return data.toString();
    }

    /**
     * 向文件中写入数据
     *
     * @param path     文件路径
     * @param json     json数据
     * @param fileName 文件名称
     * @param directoryPath 文件夹名称 
     * C盘根目录不让创建文件,只能先创建文件夹,在文件夹里创建文件
     */
    public static void writeJson(String path, String directoryPath, Object json, String fileName) {
        BufferedWriter bw = null;
        File file = new File(path + fileName);
        // 创建文件夹
        File directory = new File(directoryPath);
        boolean created = directory.mkdir();
//        if (created) {
//            System.out.println("文件夹创建成功");
//        } else {
//            System.out.println("文件夹创建失败");
//        }
        //如果文件不存在,则新建一个
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //写入数据
        try {
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(json.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//        System.out.println("写入成功!");
    }
    
}

2、创建文件,并写入,及读取

在需要的地方调用此工具类

// 此代码的思路是,如果从前端获取不到ip,就从文本里读取

            String fileName="js.txt";
            String path="D:\\js\\";
            String directoryPath = "D:\\js";
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ip", ip);

 
            String json = null;
            if(ip == "" || ip == null){
            //调用读的方法
                json = JsonUtil.readJson("D:\\js\\js.txt");
                JSONObject jsonIp = JSON.parseObject(json);
                ip = (String)jsonIp.get("ip");
            }else {
            // 调用写的方法
                JsonUtil.writeJson(path,directoryPath,jsonObject,fileName);
            }

最终在D盘创建一个js文件夹,在js文件夹内创建一个js的txt文本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖成范德彪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值