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

1、工具类

java 复制代码
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、创建文件,并写入,及读取

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

java 复制代码
// 此代码的思路是,如果从前端获取不到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文本

相关推荐
云云只是个程序马喽1 分钟前
海外短剧系统开发_云微传媒:多语言短剧平台定制与变现解决方案
java·php
梦想的颜色1 分钟前
TypeScript 完全指南(中):函数、接口、类与高级类型
前端·typescript
鹏多多3 分钟前
OpenSpec+SDD规范驱动AI Agent开发项目实战指南
前端·vue.js·react.js
叶小树咯9 分钟前
React 为什么不能像 Vue 那样 state.count++
前端·react.js
plainGeekDev14 分钟前
RecyclerView.Adapter → ListAdapter
java·kotlin·gradle
ricardo197314 分钟前
防抖节流进阶 + requestAnimationFrame:滚动与输入场景的性能优化
前端·面试
wjj不想说话14 分钟前
你项目里的 Pinia,可能已经成了第二个 localStorage
前端·vue.js
wuhen_n22 分钟前
LangChain JS 入门:快速搭建前端 AI 开发环境
前端·langchain·ai编程
J2虾虾24 分钟前
Spring AI Alibaba - 人工介入(Human-in-the-Loop)
java·人工智能·spring
Old Uncle Tom1 小时前
Harness Engineering 综述
java·开发语言·数据库