Created
November 29, 2020 09:16
-
-
Save edward1986/64c7e41a036ec0fe69b93a1fab6ccbea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<EditText | |
android:id="@+id/editText" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:ems="10" | |
android:inputType="textMultiLine" | |
app:layout_constraintBottom_toTopOf="@+id/buttonRead" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/buttonRead" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="readFile" | |
android:text="Read" | |
app:layout_constraintBottom_toTopOf="@+id/buttonWrite" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" /> | |
<Button | |
android:id="@+id/buttonWrite" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="writeFile" | |
android:text="Write" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.internalstorage; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
import java.io.BufferedReader; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
public class MainActivity extends AppCompatActivity { | |
private final String FILENAME="testfile.txt"; | |
EditText mEditText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mEditText = (EditText) findViewById(R.id.editText); | |
} | |
public void writeFile(View view) { | |
try { | |
FileOutputStream fileOutputStream = openFileOutput(FILENAME, Context.MODE_PRIVATE); | |
fileOutputStream.write(mEditText.getText().toString().getBytes()); | |
fileOutputStream.close(); | |
} catch (java.io.IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void readFile(View view) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
try { | |
InputStream inputStream = openFileInput(FILENAME); | |
if (inputStream != null) { | |
InputStreamReader inputStreamReader = new InputStreamReader(inputStream); | |
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); | |
String newLine = null; | |
while ((newLine = bufferedReader.readLine()) != null) { | |
stringBuilder.append(newLine + "\n"); | |
} | |
inputStream.close(); | |
} | |
} catch (java.io.IOException e) { | |
e.printStackTrace(); | |
} | |
mEditText.setText(stringBuilder); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment