title: Write and read binary files in Android
body: <p>I created a custom object of type <code>Task</code> and I want to save it in a binary file in internal storage. Here is the class I created:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Task {
  
      private String title;
      private int year;
      private int month;
      private int day;
      private int hour;
      private int minute;
  
      public Task(String inputTitle, int inputYear, int inputMonth, int inputDay, int inputHour, int inputMinute) {
  
          this.title = inputTitle;
          this.year = inputYear;
          this.month = inputMonth;
          this.day = inputDay;
          this.hour = inputHour;
          this.minute = inputMinute;
  
      }
  
      public String getTitle() {
  
          return this.title;
  
      }
  
      public int getYear() {
  
          return this.year;
  
      }
  
      public int getMonth() {
  
          return this.month;
  
      }
  
      public int getDay() {
  
          return this.day;
  
      }
  
      public int getHour() {
  
          return this.hour;
  
      }
  
      public int getMinute() {
  
          return this.minute;
  
      }
  }
  </code></pre>
  
  <p>In an activity, I created a method that will save my object to a file. This is the code I used:</p>
  
  <pre class="lang-java prettyprint-override"><code>public void writeData(Task newTask) {
      try {
          FileOutputStream fOut = openFileOutput("data", MODE_WORLD_READABLE);
          fOut.write(newTask.getTitle().getBytes());
          fOut.write(newTask.getYear());
          fOut.write(newTask.getMonth());
          fOut.write(newTask.getDay());
          fOut.write(newTask.getHour());
          fOut.write(newTask.getMinute());
          fOut.close();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Now I would like to create a method that will extract the data from the file. By reading on the internet, a lot of people use <code>FileInputStream</code> but I have trouble with extracting the bytes from it and knowing how long a String can be. Furthermore, I used a simple method found online but I get permission denied. As I said, I am very new to Android development.</p>
  
  <pre class="lang-java prettyprint-override"><code>public void readData(){
      FileInputStream fis = null;
      try {
          fis = new FileInputStream("data");
          System.out.println("Total file size to read (in bytes) : "
                  + fis.available());
          int content;
          while ((content = fis.read()) != -1) {
              // convert to char and display it
              System.out.print((char) content);
          }
  
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              if (fis != null)
                  fis.close();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Any help will be appreciated.</p>