Hutool 39 File

Hutool includes its own fast JSON parser. It is often preferred for simple tasks over Jackson or Gson due to its ease of use.

import cn.hutool.json.JSONUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONArray;
// 1. Parse String
JSONObject json = JSONUtil.parseObj("\"name\": \"Hutool\", \"version\": 5.8");
// 2. Get values
String name = json.getStr("name");
double version = json.getDouble("version");
// 3. Object to JSON (Serialization)
User user = new User("Admin", 25);
String jsonStr = JSONUtil.toJsonStr(user);

int randInt = RandomUtil.randomInt(1, 100);
String uuid = RandomUtil.randomUUID();
String numbers = RandomUtil.randomNumbers(6);
// Read file as string
String content = FileUtil.readUtf8String("test.txt");

// Write FileUtil.writeUtf8String("content", "test.txt"); hutool 39

// Copy/Move FileUtil.copy("src", "dest", true); Hutool includes its own fast JSON parser

String ip = ServletUtil.getClientIP(request);

  • Null Safety: Hutool methods are generally null-safe, but always check the JavaDoc. StrUtil.isEmpty(null) returns true safely.
  • Don't reinvent the wheel: Before writing a utility method, search the Hutool documentation. It likely already exists.
  • Discover more from RVS Data Conversion

    Subscribe now to keep reading and get access to the full archive.

    Continue reading