How to read and write files to my Android device?

In the app that I am creating I would like to write some text to a file and store it on the device where I can later access that file through the file explorer on the device. Is there a way to do this? I tried implementing this using a fileOutputStream but it seems that this writes the file to somewhere in the device storage which I cannot access through the file explorer. I can however view this file through the device file explorer on Android Studio when my device is connected for debugging.

This is the code that I am currently using to write files but it does not store the file where I can view it on the device.

val file = "code.py"
val fileOutputStream = FileOutputStream

try
            {
                fileOutputStream = openFileOutput(file, Context.MODE_PRIVATE)
                for(item in list)
                {
                    val opcode = LookUp()
                    var code: String = ""
                    code = opcode.match(item)

                    fileOutputStream.write(code.toByteArray())
                }
            }
            catch(e: FileNotFoundException)
            {
                e.printStackTrace()
            }
            catch(e:NumberFormatException)
            {
                e.printStackTrace()
            }
            catch(e: IOException)
            {
                e.printStackTrace()
            }
            catch(e:Exception)
            {
                e.printStackTrace()
            }
            Toast.makeText(this, "Generating", Toast.LENGTH_SHORT).show()
        }

What I am trying to achieve is writing a file to the device where I can then exit the app and go into the file explorer, to the app directory to view the file. Can anyone give me some advice on how I can accomplish this? Any assistance is appreciated.

Thanks to the bot for flagging my post as spam it got lost. Can anyone give me some advice on how I could get this to work?