将字符串转化为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文本

相关推荐
无心水21 分钟前
Java时间处理封神篇:java.time全解析
java·开发语言·python·架构·localdate·java.time·java时间处理
wuyikeer25 分钟前
Spring BOOT 启动参数
java·spring boot·后端
多看书少吃饭34 分钟前
Vue + Java + Python 打造企业级 AI 知识库与任务分发系统(RAG架构全解析)
java·vue.js·笔记
博傅1 小时前
Kubernetes (K8s) 入门到实战教程
java
~无忧花开~1 小时前
React生命周期全解析
开发语言·前端·javascript·react.js·前端框架·react
奋斗的老史1 小时前
Stream-流式操作
java·windows
cj81401 小时前
Prompt,Agent,Skill,Mcp分别于langchain有什么关系
前端
清风徐来QCQ2 小时前
八股文(1)
java·开发语言
zdl6862 小时前
springboot集成onlyoffice(部署+开发)
java·spring boot·后端