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();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM