// 單向光閘寫入文件線程 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();
