As simply as possible how do i write to a text file?

edited May 2016 in How To...

Making a game and i need to write to a/some text file(s). i need to be able to save no more than a single string to a file. How could i most simply write to and read from a text file and store the information in variables?

Answers

  • edited November 2013 Answer ✓

    You can try the simple saveStrings() + loadStrings().
    Or something more advanced like saveTable() + loadTable().

  • edited November 2013

    why would this give me a java.lang.NullPointerException?

      Table save;
    
    
      void savefile() {
        save = new Table();
    
        save.addColumn("map");
        save.addColumn("health");
        save.addColumn("stamina");
        save.addColumn("mana");
        save.addColumn("level");
        save.addColumn("strength");
        save.addColumn("defense");
        save.addColumn("melee");
        save.addColumn("magic");
        save.addColumn("ornothology");
    
        TableRow newRow = save.addRow();
        newRow.setInt("map", map);
        newRow.setInt("health", health);
        newRow.setInt("stamina", stamina);
        newRow.setInt("mana", mana);
        newRow.setInt("level", level);
        newRow.setInt("strength", strength);
        newRow.setInt("defense", defense);
        newRow.setInt("melee", melee);
        newRow.setInt("magic", magic);
        newRow.setInt("ornothology", ornothology);
    
        saveTable(save, "data/01927647864/02758.tcok");
      }
    
  • You are attempting to set what I assume to be variables as integers, but they have not been declared in your code. They are therefore null.

  • map, health, stamina, mana, level, strength, defense, melee, magic, ornothology are already defined elsewhere as integers

  • Are they defined globally? Otherwise your savefile() function won't be able to see them.

  • edited November 2013

    they are indeed defined globally like this;

      int health = 100;
      int stamina = 100;
      int mana = 100;
      //Levelables
      int level = 1;
      int strength = 1;
      int defense = 1;
      int melee = 1;
      int magic = 1;
      int ornothology = 1;
      int map = 1;
    
      Table save;
    
    
    
      void savefile() {
        save = new Table();
    
        save.addColumn("map");
        save.addColumn("health");
        save.addColumn("stamina");
        save.addColumn("mana");
        save.addColumn("level");
        save.addColumn("strength");
        save.addColumn("defense");
        save.addColumn("melee");
        save.addColumn("magic");
        save.addColumn("ornothology");
    
        TableRow newRow = save.addRow();
        newRow.setInt("map", map);
        newRow.setInt("health", health);
        newRow.setInt("stamina", stamina);
        newRow.setInt("mana", mana);
        newRow.setInt("level", level);
        newRow.setInt("strength", strength);
        newRow.setInt("defense", defense);
        newRow.setInt("melee", melee);
        newRow.setInt("magic", magic);
        newRow.setInt("ornothology", ornothology);
    
        saveTable(save, "data/01927647864/02758.tcok");
      }
    
  • If I run your code, I don't get a NPE, but an IllegalArgumentException because saveTable() doesn't recognize the extension, so doesn't know which format to use for saving.

    Should be:

    saveTable(save, "data/01927647864/02758.tcok", "csv");
    

    for example.

  • ok this just isnt going to work, and i dont think it supports int's anyways, is there any way i can just save a few ints to a text document, what the hell its easier to write to a file in python.

  • nevermind on that the null was from elsewhere, my bad

Sign In or Register to comment.