Back/Spring Java

OutputStream를 이용한 파일생성

밍꿔 2020. 4. 1. 18:25


반응형

 

 

public static void writeFile(String fileName, String path, List<Map<String, Object>> list) {
        try {
            String filePath = path + fileName;
            File destdir = new File(path);
            if (!destdir.exists()) {
                destdir.mkdirs();
            }

            OutputStream output = new FileOutputStream(filePath);
            StringBuffer sb = new StringBuffer();
            JSONArray arrayStr = convertListToJson(list);

            for(int i = 0; i< arrayStr.size(); i++){
                sb.append(arrayStr.get(i).toString()+"\n");
            }

            byte[] by = sb.toString().getBytes();
            output.write(by);
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    public static JSONArray convertListToJson(List<Map<String, Object>> bankCdList) {
        JSONArray jsonArray = new JSONArray();
        for (Map<String, Object> map : bankCdList) {
            jsonArray.add(convertMapToJson(map));
        }
        return jsonArray;
    }

 

 

 

반응형