'How to write and read text in txt file from internal storage of android vitual

i am learning by myself, without training in school. i need yours help, because i got a stuff and founding all question in the website but i can not solve it. it's very simple with you i think that. i also knew i get the wrong path(connect link).

this's my code below

Please help me find the path for internal storage. i am using genymothion with android 9 API 28. and Amaze file manager.

'enter code here' Activity main:

    <Button
        android:id="@+id/btnwrite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:text="WRITE"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtnhap" />

    <EditText
        android:id="@+id/edtnhap"
        android:layout_width="366dp"
        android:layout_height="48dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:ems="10"
        android:hint="Nhập dữ liệu tại đây"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btndoc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="162dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="161dp"
        android:text="READ"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnwrite" />

    <TextView
        android:id="@+id/tvdocdulieu"
        android:layout_width="395dp"
        android:layout_height="363dp"
        android:layout_marginTop="27dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btndoc" />
</androidx.constraintlayout.widget.ConstraintLayout>
'''
'''
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String INTERNAL_PATH = Environment.getDataDirectory().getPath()+"/sdcard/";
    private static final String ACCOUNT_FILE = "WRITING AND READ TESTING.txt";

    Button doc,ghi;
    EditText nhap;
    TextView hienthi;

    Boolean w;




    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        init();
    }

    public void init(){
        doc = findViewById( R.id.btndoc );
        ghi = findViewById( R.id.btnwrite );
        nhap = findViewById( R.id.edtnhap );
        hienthi = findViewById( R.id.tvdocdulieu );

        doc.setOnClickListener( this);
        ghi.setOnClickListener( this );
    }
    @Override
    public void onClick( View v ) {
        switch (v.getId()){
            case R.id.btnwrite:
                    xulyviet();
                break;
            case R.id.btndoc:
                xulydoc();

                break;
        }
    }
    private void xulyviet() {
        try {
            String path = INTERNAL_PATH + ACCOUNT_FILE;

            File file = new File( path );

            if(!file.exists()) {
                w = file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream( file, true );
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter( fileOutputStream, "UTF-8" ); 
            outputStreamWriter.append( nhap.getText() );
            outputStreamWriter.close();
            fileOutputStream.close();
            Toast.makeText( MainActivity.this, "đã nhập", Toast.LENGTH_SHORT ).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText( this, "Không thể ghi", Toast.LENGTH_SHORT ).show();
        }
    }
    public void xulydoc(){
    }
}

enter code here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source