Login and Registration system using kotlin file handling in android studio

Asked 1 month ago
Viewed 9 times

The app is in Kotlin. So I have the UI ready and have setup two activities; one for login and one for registration I want to create a new text file when the App launches and it holds no data. The App launches with the Login activity(this is also the MainActivity) and then the user clicks the Register button to go to the RegisterActivity. Here the user enters email, pass and confirm pass fields. This info is saved in the text file. Then the user switches back to Login activity and then logs into the App.

I am at loss at figuring out how this would work. How do I start with creating a new file within the App and then add the data to it for registering and then also read from it to Login the user.

asked 1 month ago

Correct Answer

Save File Content

@Suppress("BlockingMethodInNonBlockingContext")
    @SuppressLint("SetWorldWritable")
    private fun saveFileContent() {
        val fileContent = fileContentEditText.text.toString()
        CoroutineScope(Dispatchers.Default).launch {
            try {
                FileWriter(intent.data!!.path.toString()).apply {
                    write(fileContent)
                    close()
                }
                withContext(Dispatchers.Main) {
                    Toast.makeText(
                        [email protected],
                        getString(R.string.file_saved_successfully),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            } catch (e: IOException) {
                withContext(Dispatchers.Main) {
                    Toast.makeText(
                        [email protected],
                        getString(R.string.error_while_saving),
                        Toast.LENGTH_SHORT
                    ).show()
                }
                e.printStackTrace()
            }
        }
    }

Read File Content

private fun loadFileContent() {
        if (intent?.type == "text/plain") {
            CoroutineScope(Dispatchers.Default).launch { // Perform data retrieval tasks in the background
                intent.data?.let {
                    val fileContent = StringBuilder("")
                    contentResolver.openInputStream(it)?.bufferedReader()
                        ?.forEachLine { line -> fileContent.append("$line\n") } // Add a line break for each new line
                    // Make UI updates in the UI/Main thread
                    withContext(Dispatchers.Main) {
                        if (fileContent.isBlank())
                            Toast.makeText(
                                [email protected],
                                R.string.file_is_empty,
                                Toast.LENGTH_SHORT
                            ).show()
                        fileContentTextView.text = fileContent
                        progressCircular.visibility = View.GONE
                    }
                }
                    ?: withContext(Dispatchers.Main) {
                        progressCircular.visibility = View.GONE
                        Toast.makeText(
                            [email protected], getString(R.string.file_not_found),
                            Toast.LENGTH_SHORT
                        ).show()
                    }
            }
        } else {
            Toast.makeText(this, getString(R.string.file_cannot_be_opened), Toast.LENGTH_SHORT)
                .show()
            setResult(Activity.RESULT_CANCELED)
            finish()
        }
    }

I don't know with what logic you do it, but with the technique I shared above, you can read and write files.

answered 1 month ago