title: Android studio throwing IOException: Operation not permitted
body: <p>Right now I am currently creating an app for myself that appends data to a handful of files. But when I try to create the files -- or indeed, open them at all --, the program throws <code>java.io.IOException: Operation not permitted</code>.</p>
  <p><a href="https://i.stack.imgur.com/62R7z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/62R7z.png" alt="enter image description here" /></a></p>
  <p>As you can see, storage permissions are granted. (This app is exclusively for myself, so I'm not too worried about what it would be like for other users.)</p>
  <p>Examining the filesystem directly through the phone itself or through my computer does not show these files.</p>
  <p>Note: creating the directories (i.e. <code>dir.mkdirs();</code>) does <strong>not</strong> throw an error, even if I delete the directories beforehand. Handling the files, however, does throw errors.</p>
  <p>I am running Android 11 and my Android Studio is set to work in Lollipop.</p>
  <p><code>MainActivity.java</code>:</p>
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends AppCompatActivity {
      
      public static File dir = new File(new File(Environment.getExternalStorageDirectory(), &quot;bleh&quot;), &quot;bleh&quot;);
      
      @Override protected void onCreate(Bundle savedInstanceState) {
          
          //bleh
          
          dir.mkdirs();
          dir.setWritable(true)
          addButtons();
          
      }
      
      public void addButtons() throws IOException {
          
          Option.A.init(R.id.A);
          Option.B.init(R.id.B);
          Option.C.init(R.id.C);
          Option.D.init(R.id.D);
          Option.E.init(R.id.E);
          Option.F.init(R.id.F);
          
          // This is beyond the scope of my 
          // question, but if you could also 
          // show me a better way to do this, 
          // that would be great.
          
      }
      
  }
  </code></pre>
  <p><code>Option.java</code>:</p>
  <pre class="lang-java prettyprint-override"><code>public enum Option {
      
      A, B, C, D, E, F, ;
      
      public File data;
      public PrintWriter printer;
      
      Option() {
          
          data = new File(MainActivity.dir, name().toLowerCase() + &quot;.data&quot;);
          
      }
      
      public void init(int buttonid) {
          
          try {
              
              data.createNewFile();
              printer = new PrintWriter(data);
              // Both of these lines throw this exception.
              
          } catch (IOException ex) {
              
              ex.printStackTrace(System.err);
              
          }
          
      }
      
  }
  </code></pre>