# Saving data to a .txt file

**URL:** <https://discourse.processing.org/t/saving-data-to-a-txt-file/16600>\
**Category:** Coding Questions\
**Created:** [December 24, 2019, 12:58am UTC](https://discourse.processing.org/t/saving-data-to-a-txt-file/16600 "2019-12-24T00:58:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidclue](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davidclue/32/7370_2.png) [@davidclue](https://discourse.processing.org/u/davidclue)\
**Post date:** [December 24, 2019, 12:58am UTC](https://discourse.processing.org/t/saving-data-to-a-txt-file/16600/1 "2019-12-24T00:58:12Z")

</div>

Hi, I am making a level editor for a game and have an idea on how to load level packs into the main game. The idea is to save data in the form of arrays of numbers that builds a level in a text document and then people can copy paste or download the text document and import it into a sub folder for the main game. So the problem is how could I create a manager for reading and writing the text documents in format. Lets say something like this:

```auto
Level 1: //resembles levels
    Layer 1: Time: 2.0 //spawn layers of enemies through the level, time is the delay for the next layer in seconds.
        1,200,400,1,0,25,2,2 //an array of numbers that resembles the data for an enemy x,y,size,speed,etc.
        1,800,300,-1,0,25,2,2
    Layer 2: Time: 5.0
        2,800,300,-1,0,25,2,2
        1,800,300,-1,0,25,2,2
Level 2:
    Layer 1: Time: 8.0
        5,23,424,12,5

```

And so on, so when they select a level pack (one of the .txt files from the sub folder) it will read the data like that. So how could I make it read and write data in a format like that, I am familiar with saving arrays to .txt files already it’s just I have no idea on how to make it write in that format and read it.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 24, 2019, 1:07am UTC](https://discourse.processing.org/t/saving-data-to-a-txt-file/16600/2 "2019-12-24T01:07:22Z")

</div>

for me looks like spread sheet CSV file

```auto
level , layer , time , a,b,c,d,e,f,g,h,
1,1,2.0,1,200,400,1,0,25,2,2
//...

```

```auto
Table table;
String infilename = "data/game.csv";

/*
level , layer , time , a,b,c,d,e,f,g,h
 1,1,2.0,1,200,400,1,0,25,2,2
 */

String outfile = "data/newuser.csv"; 
int trows = 0, tcols;   

// ____________________________________________________________
void setup() {
  size(500, 500);
  background(200, 200, 0);
  if ( true ) {
    get_Table(); // _____________________________________________ IF TABLE EXISTS READ
  }
  if ( true ) {
    make_Table(); // ____________________________________________ EXAMPLE TABLE MAKE FILL WRITE
    make_Line();
    saveTable(table, outfile);
  }
}

// ____________________________________________________________
void draw() {
  // background(200,200,0);
}

// ____________________________________________________________
void get_Table() {
  table=loadTable(infilename, "header");
  trows = table.getRowCount();
  tcols = table.getColumnCount();
  println("rows: "+trows+" columns: "+tcols+" in file: "+infilename);
  table.setColumnType("Level", Table.INT); // optional type setting...
  fill(0); // __________________________________________________________________ text color on canvas
  int dy =0;
  for ( int c = 0; c < tcols; c++) text( table.getColumnTitle(c)+" ", 10+30*c, 10); //___ print header line
  dy++;
  for ( int r = 0; r < trows; r++) { // ________________________________________ get data lines
    TableRow thisrow = table.getRow(r);
    for ( int c = 0; c < tcols; c++ ) text( thisrow.getString(c), 10+30*c, 10+15*dy );
    dy++;
  }
}

// _____________________________________________________________________________ options for table create / fill / save
void make_Table() {
  table = new Table();
  table.addColumn("level");
  table.addColumn("layer");
  table.addColumn("time");
  table.addColumn("a");
  table.addColumn("b");
  table.addColumn("c");
  table.addColumn("d");
  table.addColumn("e");
  table.addColumn("f");
  table.addColumn("g");
  table.addColumn("h");
}

void make_Line() {
  TableRow newRow = table.addRow();
  newRow.setInt("level", 1);
  newRow.setInt("layer", 1);
  // newRow.setString("time", ""+year()+"_"+nf(month(), 2)+"_"+nf(day(), 2)+"_"+nf(hour(), 2)+"_"+nf(minute(), 2)+"_"+nf(second(), 2));
  newRow.setFloat("time", 2.0);
  newRow.setInt("a", 1);
  newRow.setInt("b", 200);
  newRow.setInt("c", 400);
  newRow.setInt("d", 1);
  newRow.setInt("e", 0);
  newRow.setInt("f", 25);
  newRow.setInt("g", 2);
  newRow.setInt("h", 2);
}

void keyPressed() {
  if ( key == 's' ) {
    saveTable(table, outfile);
    println("save to "+outfile);
  }
}

```

 ![game_csv](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4a978e418f9f8ebda80c16528754dc3c456c5f87.png)
