title: Best way to write a text file to internal sorage?
body: <p>I'm trying to write a text file to an internal storage and wondered what's the best way would be to do it, the text file will contain strings.</p>
  
  <p>I have:</p>
  
  <pre><code>    File file = new File(getFilesDir() + subFolderName + "/" + fileName);
                  BufferedWriter writer;
  
  
                  try {
                      writer = new BufferedWriter(new FileWriter(file));
  
                      writer.write("ID, Date, Address, Body");
                      writer.write("\n");
                      for (String s : list) {
                          writer.write(s);
                          writer.write("\n");
                      }
                      writer.write("\n");
                      writer.flush();
                      writer.close();
                  } catch (IOException e) {                   
                      e.printStackTrace();
  }
  </code></pre>
  
  <p>I just get a <code>java.lang.NullPointerException</code> at the creation of the new file :/</p>
  
  <p>Thanks in advance.</p>
  
  <p>This is the code which i ended up with which worked as i wanted it to:</p>
  
  <pre><code>public void WriteFileInternal(ArrayList&lt;String&gt; list, String FileName, Context context) {
  
      BufferedWriter bw;
      try {
          ContextWrapper cw = new ContextWrapper(context);
          File directory = cw.getDir("SMSMonitor", Context.MODE_PRIVATE);
  
          if (!directory.exists()){
              directory.createNewFile();
              directory.mkdir();
  
              }
          File file = new File(directory +"/"+ FileName);
          file.createNewFile();
  
          bw = new BufferedWriter(new FileWriter(file));
          bw.write("ID, Date, Body, Phone Number");
          bw.write("\n");
  
          for (String s1 : list) {
              bw.write(s1);
              bw.write("\n");
          }
          bw.write("\n");
          bw.flush();
          bw.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>