August 16, 2018

Writing JSON to a file in java

package com.belazy.fileops;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

/**
 *
 * @author consumerfed IT
 * itatcalicut@gmail.com
 *
 */
public class WritingToFile {

public static void main(String[] args) {

WritingToFile s = new WritingToFile();

String requestStr = "{Data science is future, learn artificial intelligence}";
s.saveJSONToFile(requestStr, "consumerfed", "itesction", "kozhikode",
"8281808029");
// s.saveJSONToFile2(requestStr, "consumerfed", "itesction", "kozhikode",
// "8281808029");
}

public static void saveJSONToFile(String requestStr, String tripID,
String reqRes, String supplier, String folderName) {
PrintWriter out1 = null;
try(PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(
f.getPath() + File.separator + tripID, true)));
out1) {
String path1 = "D://beebeesotware";

File f = new File(path1 + File.separator + folderName
+ File.separator + "request");
f.mkdirs();
out1 = new PrintWriter(new BufferedWriter(new FileWriter(
f.getPath() + File.separator + tripID, true)));
out1.println(requestStr);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != out1)
out1.close();
}

}

public static void saveJSONToFile2(String requestStr, String tripID,
String reqRes, String supplier, String folderName) {
PrintWriter out1 = null;
try {
String path1 = "D://beebeesotware";

File f = new File(path1 + File.separator + folderName
+ File.separator + "request");
f.mkdirs();
out1 = new PrintWriter(new BufferedWriter(new FileWriter(
f.getPath() + File.separator + tripID, true)));
out1.println(requestStr);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != out1)
out1.close();
}

}
}

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments