java自用代码(包括:新建单线程、创建文件夹及文件、map转为json并将json写入txt、文件剪切或改名)

        // 单向光闸写入文件线程
        new Thread(new Runnable() {
            // 将map转为json
            JSONObject obdjson = new JSONObject(data);
            @Override
            public void run() {
                try {
                    //创建文件夹及文件
                    String fileName = UUID.randomUUID().toString().replaceAll("-", "");
                    String wfileName = "c:\\外网\\"+fileName+".obd";
                    String path = "E:\\test\\"+fileName+".temp";
                    File fpath = new File(path);
                    File parentFile = fpath.getParentFile();
                    if(!parentFile.exists()) 
                    {
                        parentFile.mkdirs();
                    }
                    fpath.createNewFile();
                    // 将json写入到txt中
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true)));
                    out.write(obdjson.toJSONString());
                    out.close();
                    //剪切并重命名
                    fpath.renameTo(new File(wfileName));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

 

posted @ 2019-01-08 11:34  程序员张达  阅读(1192)  评论(0编辑  收藏  举报