java RandomAccessFile 寫文件

try {
    JSONObject nodes = new JSONObject();
    JSONArray nodesArray = new JSONArray();
    JSONArray linksArray = new JSONArray();

    for(int i = 0; i <= 40; i++){
        int value = (int)(Math.random()*46);
        JSONObject object = new JSONObject();
        object.put("id", i);
        JSONObject attribute = new JSONObject();
        attribute.put("modularity_class", 1);
        object.put("attributes", attribute);
        object.put("category", new Random().nextInt(9));
        object.put("draggable", true);
        object.put("itemStyle", null);
        object.put("name", getCompanyName());
        object.put("symbolSize", value);
        object.put("value", value*2);
        object.put("x", null);
        object.put("y", null);
        nodesArray.add(object);
    }
    nodes.put("nodes", nodesArray);
    for (int i = 1; i <= 40; i++){
        JSONObject objectLink = new JSONObject();
        objectLink.put("name",null);
        objectLink.put("source", 0);
        objectLink.put("target", i);
        linksArray.add(objectLink);
    }
    nodes.put("links", linksArray);
    JSONArray finalArray = new JSONArray();
    finalArray.add(nodes);
    String path = "D:\\industry.json";
    File file = new File(path);
    if (file.exists()) file.delete();
    RandomAccessFile raf = new RandomAccessFile(path, "rws");
    raf.setLength(0);
    raf.write(finalArray.toString().getBytes("UTF-8"));
    raf.close();
}catch (Exception e){

}
相關文章
相關標籤/搜索