I have this JSON String and I need to get each docmanId and each dz so,I could loop through them and work with them.
I have tried using gson library to do that,but I dont seem to figure it out.
JSON Array :
[{"docmanId":1,"dz":"CR"},
{"docmanId":1,"dz":"EU"},
{"docmanId":1,"dz":"JD"},
{"docmanId":1,"dz":"LT"},
{"docmanId":10,"dz":"CR"},
{"docmanId":10,"dz":"EU"},
{"docmanId":10,"dz":"LT"},
{"docmanId":100,"dz":"CR"},
{"docmanId":100,"dz":"EU"},
{"docmanId":100,"dz":"JD"},
{"docmanId":100,"dz":"LT"},
{"docmanId":1000,"dz":"CR"},
{"docmanId":1000,"dz":"EU"},
{"docmanId":1000,"dz":"JD"},
{"docmanId":1000,"dz":"LT"},
{"docmanId":10000,"dz":"ES"},
{"docmanId":10000,"dz":"EU"},
{"docmanId":10000,"dz":"JD"},
{"docmanId":100000,"dz":"CR"},
{"docmanId":100000,"dz":"EU"},
{"docmanId":100000,"dz":"JD"},
{"docmanId":100000,"dz":"LT"},
{"docmanId":100001,"dz":"CR"},
{"docmanId":100001,"dz":"EU"},
{"docmanId":100001,"dz":"LT"},
{"docmanId":100002,"dz":"CR"},
{"docmanId":100002,"dz":"EU"},
{"docmanId":100002,"dz":"JD"},
{"docmanId":100003,"dz":"CR"},
{"docmanId":100003,"dz":"EU"},
{"docmanId":100003,"dz":"JD"},
{"docmanId":100003,"dz":"LT"},
{"docmanId":100004,"dz":"CR"},
{"docmanId":100004,"dz":"EU"},
{"docmanId":100004,"dz":"JD"},
{"docmanId":100005,"dz":"CR"},
{"docmanId":100005,"dz":"EU"},
{"docmanId":100005,"dz":"JD"},
{"docmanId":100005,"dz":"LT"},
{"docmanId":100006,"dz":"CR"},
{"docmanId":100006,"dz":"EU"},
{"docmanId":100006,"dz":"JD"},
{"docmanId":100006,"dz":"LT"},
{"docmanId":100007,"dz":"CR"},
{"docmanId":100007,"dz":"EU"},
{"docmanId":100007,"dz":"JD"}]
With org.json ,
JSONArray jSONArray = new JSONArray("your input array");
int length = jSONArray.length();
for (int i = 0; i < length; i++) {
JSONObject jSONObject= jSONArray.getJSONObject(i);
System.out.println(jSONObject.get("docmanId"));
System.out.println(jSONObject.get("dz"));
}
with jackson
String json = "[{\"docmanId\":1,\"dz\":\"CR\"},\n" +
"{\"docmanId\":1,\"dz\":\"EU\"},\n" +
"{\"docmanId\":1,\"dz\":\"JD\"},\n" +
"{\"docmanId\":1,\"dz\":\"LT\"},\n" +
"{\"docmanId\":10,\"dz\":\"CR\"},\n" +
"{\"docmanId\":10,\"dz\":\"EU\"},\n" +
"{\"docmanId\":10,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100,\"dz\":\"LT\"},\n" +
"{\"docmanId\":1000,\"dz\":\"CR\"},\n" +
"{\"docmanId\":1000,\"dz\":\"EU\"},\n" +
"{\"docmanId\":1000,\"dz\":\"JD\"},\n" +
"{\"docmanId\":1000,\"dz\":\"LT\"},\n" +
"{\"docmanId\":10000,\"dz\":\"ES\"},\n" +
"{\"docmanId\":10000,\"dz\":\"EU\"},\n" +
"{\"docmanId\":10000,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100000,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100000,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100000,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100000,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100001,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100001,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100001,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100002,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100002,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100002,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100003,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100003,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100003,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100003,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100004,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100004,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100004,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100005,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100005,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100005,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100005,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100006,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100006,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100006,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100006,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100007,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100007,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100007,\"dz\":\"JD\"}]";
ObjectMapper objectMapper = new ObjectMapper();
DocmanList docmanList = objectMapper.readValue(json, DocmanList.class);
//logic below
}
public class Docman {
private long docmanId;
private String dz;
public long getDocmanId() {
return docmanId;
}
public void setDocmanId(long docmanId) {
this.docmanId = docmanId;
}
public String getDz() {
return dz;
}
public void setDz(String dz) {
this.dz = dz;
}
}
public class DocmanList extends ArrayList<Docman> {
}
you can do it by generating a class convert it in java object of list.
first generate a class
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Example implements Serializable
{
#SerializedName("docmanId")
#Expose
private long docmanId;
#SerializedName("dz")
#Expose
private String dz;
private final static long serialVersionUID = 3195470113916852896L;
/**
* No args constructor for use in serialization
*
*/
public Example() {
}
/**
*
* #param docmanId
* #param dz
*/
public Example(long docmanId, String dz) {
super();
this.docmanId = docmanId;
this.dz = dz;
}
public long getDocmanId() {
return docmanId;
}
public void setDocmanId(long docmanId) {
this.docmanId = docmanId;
}
public Example withDocmanId(long docmanId) {
this.docmanId = docmanId;
return this;
}
public String getDz() {
return dz;
}
public void setDz(String dz) {
this.dz = dz;
}
public Example withDz(String dz) {
this.dz = dz;
return this;
}
#Override
public String toString() {
return new ToStringBuilder(this).append("docmanId", docmanId).append("dz", dz).toString();
}
#Override
public int hashCode() {
return new HashCodeBuilder().append(docmanId).append(dz).toHashCode();
}
#Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Example) == false) {
return false;
}
Example rhs = ((Example) other);
return new EqualsBuilder().append(docmanId, rhs.docmanId).append(dz, rhs.dz).isEquals();
}
}
Now Tell it to parse a List (of Welcome) instead. Since List is generic you will typically use a **TypeReference**
List<Welcome> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Welcome>>(){});
use http://www.jsonschema2pojo.org/ to convert your json to java class.
You could parse it using JsonPath :
static String json = "...";
public static void main(String[] args) {
String pageName = JsonPath.read(json, "$.pageInfo.pageName");
System.out.println(pageName);
Integer posts = JsonPath.read(json, "$.posts.length()");
for(int i=0; i < posts; i++) {
String post_id = JsonPath.read(json, "$.posts[" + i + "].post_id");
System.out.println(post_id);
}
}
I have two methods upsert into couchbase. Then I write two Junit tester with springboottest. After one Junit tester completed another test will throw this exception. How to resolve?
There are two upsert methods:I don't know which one methods is better?
public List<RawJsonDocument> upsert2(String generatorId, String idPrefix, List<String> contents)
{
List<RawJsonDocument> rjd = new ArrayList<RawJsonDocument>(contents.size());
Observable.from(contents).flatMap(new Func1<String,Observable<String>>(){
#Override
public Observable<String> call(String t)
{
return bucket.async().counter(generatorId, 1)
.map(jsonLongDocument -> {
String idStr = idPrefix + generatorId + jsonLongDocument.content();
String jsonStr = idStr + "=" + t;
return jsonStr;
});
}}).subscribe(new Action1<String>() {
#Override
public void call(String t)
{
String[] s = t.split("[=]");
LOGGER.debug("\n methord2 generatorId:" + s[0] + "\n content:" + s[1]);
bucket.async().upsert(RawJsonDocument.create(s[0],s[1]));
}});
return rjd;
}
public List<RawJsonDocument> upsert1(String generatorId, String idPrefix, List<String> contents)
{
if(contents == null)
{
return null;
}
List<RawJsonDocument> rjd = new ArrayList<RawJsonDocument>(contents.size());
Observable.from(contents).flatMap(new Func1<String,Observable<RawJsonDocument>>(){
#Override
public Observable<RawJsonDocument> call(String t)
{
return bucket.async().counter(generatorId, 1)
.map(jsonLongDocument -> {
String idStr = idPrefix + generatorId + jsonLongDocument.content();
LOGGER.debug("\n method3 generatorId:" + idStr + "\n content:" + t);
return RawJsonDocument.create(idStr,t);
});
}}).subscribe(new Action1<RawJsonDocument>() {
#Override
public void call(RawJsonDocument t)
{
rjd.add(bucket.async().upsert(t).toBlocking().single());
}});
return rjd;
}
This is my Junit Tester:
#Test
public void testIncrementIds3()
{
assertThat(generatorId.upsert2("counter", "idprefix", Arrays.asList("aabbccdd","ffddeeaa")).size(),is(2));
assertThat(generatorId.upsert1("counter", "idprefix", Arrays.asList("aabbccdd","ffddeeaa")).size(),is(2));
}
Class1.java file:
public class Class1{
private ArrayList<Class2> class2List;
...
}
Class2.java file:
public class Class2{
private Point point = new Point();
private ArrayList<Point> pointList;
...
}
These are my classes, and I want to parse a Class1 object to JSON string with GSON.
This works fine:
Gson gson = new Gson();
String json = gson.toJson(class1_object_name);
And generates something like that:
{
"class2List":[
{
"point":{"x":131,"y":304},
"pointList":
[
{"x":134,"y":319},
{"x":135,"y":333},
{"x":133,"y":348},
{"x":129,"y":349}
]
},
{
"point":{"x":311,"y":277},
"pointList":
[
{"x":312,"y":279},
{"x":315,"y":286},
{"x":318,"y":302},
{"x":321,"y":328},
{"x":321,"y":353}
]
}
]
}
But I don't know how to create (decode) this object-structure later from the JSON string.
Or the only way to decode is to manually loop through the String, and create all of the objects?
I couldn't find the problem with your code. This worked for me.
POJO classes
PointData.java
public class PointData {
private List<Class2List> class2List = new ArrayList<Class2List>();
public List<Class2List> getClass2List() {
return class2List;
}
public void setClass2List(List<Class2List> class2List) {
this.class2List = class2List;
}
}
Class2List.java
public class Class2List {
private Point point;
private List<PointList> pointList = new ArrayList<PointList>();
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public List<PointList> getPointList() {
return pointList;
}
public void setPointList(List<PointList> pointList) {
this.pointList = pointList;
}
}
Point.java
public class Point {
private Integer x;
private Integer y;
public Integer getX() {
return x;
}
public void setX(Integer x) {
this.x = x;
}
public Integer getY() {
return y;
}
public void setY(Integer y) {
this.y = y;
}
}
PointList.java
public class PointList {
private Integer x;
private Integer y;
public Integer getX() {
return x;
}
public void setX(Integer x) {
this.x = x;
}
public Integer getY() {
return y;
}
public void setY(Integer y) {
this.y = y;
}
}
JSON Parsing code
String data = "{\n" +
" \"class2List\":[\n" +
" {\n" +
" \"point\":{\"x\":131,\"y\":304},\n" +
" \"pointList\":\n" +
" [\n" +
" {\"x\":134,\"y\":319},\n" +
" {\"x\":135,\"y\":333},\n" +
" {\"x\":133,\"y\":348},\n" +
" {\"x\":129,\"y\":349}\n" +
" ]\n" +
" },\n" +
"\n" +
" {\n" +
" \"point\":{\"x\":311,\"y\":277},\n" +
" \"pointList\":\n" +
" [\n" +
" {\"x\":312,\"y\":279},\n" +
" {\"x\":315,\"y\":286},\n" +
" {\"x\":318,\"y\":302},\n" +
" {\"x\":321,\"y\":328},\n" +
" {\"x\":321,\"y\":353}\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
PointData parsedData = new Gson().fromJson(data, PointData.class);
Log.e("JSON", parsedData.getClass2List().get(0).getPoint().getX().toString());
build.gradle
compile 'com.google.code.gson:gson:2.8.0'
You can use gson.fromJson(jsonString,Class)
In your case will be something like this:
Class1 obj=gson.fromJson(stringJson,Class1.class)
How to handle file data as shown below, which is delimited by uneven whitespaces, where if tokenizer is used based on space, it'll give tokens which cannot be assigned to java bean fields directly.
Below is the content Data of cheapdata4.data:
(TX 260816.<
0954F2003EGGPLEIBLNX37PU ZC550 Z <CA C A L L8 STAF P18 UL15 KIDL
0001F0148BIKFEDDSGWI2797 ZA319 Z <CATGWI2 C M V104 GMH4 GMH UL60 EDDS
4893F1416EGPGEGHFGJOID ZSR20 Z <AAEGPGD C A LT TLA DCS L612 N859 Q41
7945F1400EGSHEGCCLOG63JF ZD328 Z <(A C A R L OTBE Y70 EGCC
7946F1647EGSHEGGWAZE01F ZE50P Z <(A C A R LT MAM1 LAPR ABBO BKY2
7947F1701EGSHEGCCLOG63JF ZD328 Z <(A C A R L / MAM0 OTBE POL ROSU
4368F1657ESSBEGGWBLJ59BF ZC56X Z <RAUBLJ5 A LT UN86 UP7 UP25 EGGW
4369F1728ESSBEGCCETI226L MLJ45 012<RAUETI2 A L UL97 Y70 EGCC
4370F0551LHBPEGGWWZZ196 ZA321 Z <RAUWZZ1 A MT UL60 UY6 UM20 EGGW
7950END 260816.<
package com.msa.parser;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.msa.bean.CheapData;
public class FinalParser {
public static void main(String[] args) {
BufferedReader reader;
try {
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(
new FileReader(
"C:\\Users\\Desktop\\assignment\\cheapdata4.data"));
System.out.println("Reading file...");
String line = reader.readLine();
line = line.replaceAll("\\s{2,}", " ");
String[] toks = line.replace(" ", ",").trim().split(",");
line = line.replaceAll("\\s{2,}", " ");
String[] headerTokens = line.split(" +");
String fileStartDate = headerTokens[1].substring(0, 6);
List<String> cheapDataContent = new ArrayList<>();
String[] lineTokens = { "" };
CheapData cheapFileDets = null;
List<CheapData> cheapDataList = new ArrayList<>();
while (line != null) {
line = reader.readLine();
line = line.replaceAll("\\s{2,4}", " ");
// line = line.replaceAll("\\s{2}", " NA ");
// line = line.replaceAll(" ",", ");
// line = line.replaceAll("\\,{2}", "");
lineTokens = line.split(" +");
int len = lineTokens.length;
// String fileEndDate ="";
if (len == 2) {
String fileEndDate = lineTokens[1].substring(0, 6);
if (fileStartDate.equals(fileEndDate)) {
break;
} else {
System.out.println("Date mismatch...");
}
}
if (len >= 6) {
cheapFileDets = new CheapData();
String lineNum = lineTokens[0].substring(0, 4);
cheapFileDets.setLineNum(lineNum);
String cheapReference = lineTokens[0].substring(4);
cheapFileDets.setCheapReference(cheapReference);
cheapFileDets.setDate(fileStartDate);
cheapFileDets.setAircraftCode(lineTokens[1]);
cheapFileDets.setIdentifierCode(lineTokens[2]);
cheapFileDets.setLicenseCode(lineTokens[3]);
if (lineTokens[4].equals("C"))
cheapFileDets.setCodeOne(lineTokens[4]);
else
cheapFileDets.setCodeOne(null);
if (lineTokens[5].equals("A"))
cheapFileDets.setCodeTwo(lineTokens[5]);
else
cheapFileDets.setCodeTwo(null);
if (lineTokens[6].equals("R"))
cheapFileDets.setCodeThree(lineTokens[6]);
else
cheapFileDets.setCodeThree(null);
} else {
if (len == 7)
cheapFileDets.setIdCode(lineTokens[7]);
else
cheapFileDets.setIdCode(null);
}
List<String> miscList = new ArrayList<>();
for (int i = 7; i < len - 1; i++) {
miscList.add(lineTokens[i]);
}
StringBuilder miscAll = new StringBuilder("");
for (String miscs : miscList) {
miscAll.append(miscs + " ");
}
cheapFileDets.setMiscAll(miscAll.toString());
cheapDataList.add(cheapFileDets);
}
System.out.println("File content" + sb.toString());
System.out.println("file date is: " + fileStartDate);
System.out.println("*************");
/*
* for (String strToks : lineTokens) { System.out.println(strToks);
* }
*/
// System.out.println(cheapDataList);
Iterator<CheapData> itrCheapData = cheapDataList.listIterator();
while (itrCheapData.hasNext()) {
System.out.println(itrCheapData.next());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
package com.msa.bean;
public class CheapData {
private String lineNum;
private String date;
private String cheapReference;
private String aircraftCode;
private String identifierCode;
private String licenseCode;
private String codeOne;
private String codeTwo;
private String codeThree;
private String idCode;
private String miscAll;
public String getLineNum() {
return lineNum;
}
public void setLineNum(String lineNum) {
this.lineNum = lineNum;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCheapReference() {
return cheapReference;
}
public void setCheapReference(String cheapReference) {
this.cheapReference = cheapReference;
}
public String getAircraftCode() {
return aircraftCode;
}
public void setAircraftCode(String aircraftCode) {
this.aircraftCode = aircraftCode;
}
public String getIdentifierCode() {
return identifierCode;
}
public void setIdentifierCode(String identifierCode) {
this.identifierCode = identifierCode;
}
public String getLicenseCode() {
return licenseCode;
}
public void setLicenseCode(String licenseCode) {
this.licenseCode = licenseCode;
}
public String getCodeOne() {
return codeOne;
}
public void setCodeOne(String lineTokens) {
this.codeOne = lineTokens;
}
public String getCodeTwo() {
return codeTwo;
}
public void setCodeTwo(String lineTokens) {
this.codeTwo = lineTokens;
}
public String getCodeThree() {
return codeThree;
}
public void setCodeThree(String codeThree) {
this.codeThree = codeThree;
}
public String getIdCode() {
return idCode;
}
public void setIdCode(String idCode) {
this.idCode = idCode;
}
public String getMiscAll() {
return miscAll;
}
public void setMiscAll(String miscAll) {
this.miscAll = miscAll;
}
public CheapData(String lineNum, String date, String cheapReference,
String aircraftCode, String identifierCode, String licenseCode,
String codeOne, String codeTwo, String codeThree, String idCode,
String miscAll) {
super();
this.lineNum = lineNum;
this.date = date;
this.cheapReference = cheapReference;
this.aircraftCode = aircraftCode;
this.identifierCode = identifierCode;
this.licenseCode = licenseCode;
this.codeOne = codeOne;
this.codeTwo = codeTwo;
this.codeThree = codeThree;
this.idCode = idCode;
this.miscAll = miscAll;
}
#Override
public String toString() {
return "CheapData [lineNum=" + lineNum + ", date=" + date
+ ", cheapReference=" + cheapReference + ", aircraftCode="
+ aircraftCode + ", identifierCode=" + identifierCode
+ ", licenseCode=" + licenseCode + ", codeOne=" + codeOne
+ ", codeTwo=" + codeTwo + ", codeThree=" + codeThree
+ ", idCode=" + idCode + ", miscAll=" + miscAll + "]";
}
public CheapData() {
super();
}
}
Please can anyone help me out in this regard. I want to parse above file and populate each columns from that to an object using setters in java.
Please do suggest me how to achieve this and kindly let me know if my query not clear.
I have a jtable that can be edite and then saved (updated) to a text file.
User select a line (that contains a book record) and request to borrow that book,
I use this method to update, But now when update, the old data is not deleted.
user_AllBooks uAllBooks = new user_AllBooks();
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == borrowButton) {
borrowInitialize(bTable.getSelectedRow());
}
public void borrowInitialize(int row) {
if (uAllBooks.getValueAt(row, 3).equals("Yes")) {
JOptionPane.showMessageDialog(null, "This Book Was Borrowed");
} else {
uAllBooks.setValueAt("Yes", row, 3);
uAllBooks.fireTableRowsUpdated(row, row);
uAllBooks.updateFiles(uAllBooks.bData);
}
}
...
}
public class user_AllBooks extends AbstractTableModel {
...
public void updateFiles(ArrayList<BookInformation> data) {
PrintWriter Bpw = null;
try {
Bpw = new PrintWriter(new FileWriter("AllBookRecords.txt" , true));
for (BookInformation bookinfo : data) {
String line = bookinfo.getBookID()
+ " " + bookinfo.getBookName()
+ " " + bookinfo.getBookDate()
+ " " + bookinfo.getBorrowStatus();
Bpw.println(line);
}
Bpw.close();
} catch (FileNotFoundException e1) {
} catch (IOException ioe) {
}
}
...
}
My BookInformation Class:
public class BookInformation {
private String BookName;
private String BookDate;
private String BookID;
private String BorrowStatus;
public String getBookName() {
return BookName;
}
public void setBookName(String book_name) {
this.BookName = book_name;
}
public String getBookDate() {
return BookDate;
}
public void setBookDate(String book_date) {
this.BookDate = book_date;
}
public String getBookID() {
return BookID;
}
public void setBookID(String Book_id) {
this.BookID = Book_id;
}
#Override
public String toString() {
return BookID + " " + BookName + " "
+ BookDate + " " + BorrowStatus + "\n";
}
public String getBorrowStatus() {
return BorrowStatus;
}
public void setBorrowStatus(String borrowStat) {
BorrowStatus = borrowStat;
}
}
Thanks.
Change this line
Bpw = new PrintWriter(new FileWriter("AllBookRecords.txt" , true));
to
Bpw = new PrintWriter(new FileWriter("AllBookRecords.txt" , false));
The second parameter (boolean) changes whether it should append the text file (add to the end of it) or just rewrite everything.
Source: Javadoc constructor summary for FileWriter:
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean
indicating whether or not to append the data written.