How to save an integer in a file

TypTyp
edited March 2016 in How To...

Hello, I already know this code: "saveStrings("myData.txt", Name);. But with that you only can save strings, I think? So now I'd like to know the code to save Integer.

Hopefully someone can help me

greetings Typ

Tagged:

Answers

  • Yeah, I already found out about that, but this solves not my problem, I MUST save it as an integer and not as string. This has something to do with another programm. But thanks anyway!

  • No.....

    I mean.... in the text file it is the same

    Test it, it will work

  • This is a example code:

    /*#############################################################    ################################################
          Processing help functions for file operations By Erkosone: Luis Lopez Martinez.
          Version 1.0 - for Processing 3.0.1 / 3.0.2.
          TEMPLATE example: For Windows, Linux and MacOSX..
        #############################################################################################################*/
        // Global var data..
        String file = "myFile.bin";
        int st = 0;  // state machine for cycle control..
        int value = 32000;
        // App settings..
        void settings() {
          size(640,400,P2D);
        }
        // onStart..
        void setup(){
        }
        // App cycle..
        void draw(){
          switch(st){
            case 0:
              // INITIAL STATE..
              if(!fileExists(dataPath(file))){
                // file not exists.. make it..
                st = 100;
              }
              else{
                // file exists!! read it..
                st = 200;
              }
              break;
              //--------------------
            case 100:
              // FILE NOT EXISTS.. CREATE IT..
              createFile(dataPath(file));
              st = 110;
              break;
              //--------------------
            case 110:
              // WRITE INT TO FILE..
              byte[] buffer = new byte[2];            // prepare byte array..
              buffer = getBytesFromInt(value);        // insert bytes from int to array..
              fwrite(dataPath(file), buffer, false);  // write bytes to file.. 3º parameter in false for not append data..
              st = 120;
              break;
              //--------------------
            case 120:
              // READY..
              background(0);
              text("2 BYTES WRITED ON FILE" + file + "!", 20, 50);
              st = 999;
              break;
              //--------------------
              //--------------------
              //--------------------
            case 200:
              // READ FILE FROM DISK..
              byte[] buffer2 = null;                  // prepare a void array..
              buffer2 = fread(dataPath(file));        // read bytes from disk..
              int parsedInt = getWord(buffer2, 0);    // parse byte[] to integer.. second parameter indicates the offset to get bytes..
              println(parsedInt);
              st = 210;
              break;
              //--------------------
            case 210:
              // READY..
              background(0);
              text("2 BYTES READED FROM FILE" + file + "!", 20, 50);
              st = 999;
              break;
              //--------------------
              //--------------------
              //--------------------
          }
    
        }
    //######################################################################
    //######################################################################
    //######################################################################
    // transforms 2 bytes in a integer..
    final int getWord(byte[] arr, int off) {
      return (arr[off] & 0xff) + (256 * (arr[off+1] & 0xff));
    }
    //----------------------------------------------------------------------
    // transforms integfer in a 2 bytes..
    final byte[] getBytesFromInt(int val) {
      byte[] data = new byte[2];
      data[0] = (byte) (val & 0xFF);
      data[1] = (byte) ((val >> 8) & 0xFF);
      return data;
    }
    //----------------------------------------------------------------------
    import java.nio.file.*;
    import java.io.*;
    import static java.nio.file.StandardOpenOption.*;
    //----------------------------------------------------------------
    // write array of bytes on a file "in bynary mode"..
    final void fwrite(String filename, byte[] data, boolean append){
      Path file = Paths.get(filename);
      OutputStream output = null;
        try
        {
          if(append){
            output = new BufferedOutputStream(Files.newOutputStream(file, APPEND));
          }
          else{
            File f = new File(filename);
            f.delete();
            output = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
          }
          output.write(data);
          output.flush();
          output.close();
        }
        catch(Exception e)
        {
            System.out.println("Message: " + e);
        }
    }
    //----------------------------------------------------------------------
    // read array of bytes from disk..
    import java.io.RandomAccessFile;
    final byte[] fread(String fileName_){
      RandomAccessFile f = null;
      byte[] buffer = null;
      try{
        f = new RandomAccessFile(dataPath(fileName_), "rw");
        buffer = new byte[(int)f.length()];
        f.read(buffer);
      }
      catch(Exception e){
      }
      return buffer;
    }
    //----------------------------------------------------------------------
    // create file in disk..
    final void createFile(String filename){
      BufferedWriter bw = null;
      try {  
        FileWriter fw = new FileWriter(filename, true);
        bw = new BufferedWriter(fw);
        bw.write("");
      } catch (IOException e) {
      } finally {
        if (bw != null){
          try { 
            bw.close(); 
          } catch (IOException e) {}  
        }
      }
    }
    //----------------------------------------------------------------
    // copu disk file in bynary mode..
    final void copyFile(String sourceFile, String destFile){
      byte[] source = loadBytes(sourceFile);
      saveBytes(destFile, source);
    }
    //----------------------------------------------------------------
    // create a folder in disk..
    final boolean mkDir(String folderName){
      File dir = new File(folderName);
      dir.mkdir();
      return fileExists(folderName);
    }
    //----------------------------------------------------------------
    final boolean fileExists(String filename) {
      File file=new File(filename);
      boolean exists = file.exists();
      if (exists) {
        return true;
      }
      else {
        return false;
      }
    }
    
Sign In or Register to comment.