Another feature when you work with files in Internal Storage, you can only work with a simple file name, can not work with a file name has the path.

Bạn đang xem: Internal storage là gì

// Is a simple file name.// Note!! vì not allow the path.String simpleFileName ="note.txt";// xuất hiện Stream lớn write file.FileOutputStream out = openFileOutput(simpleFileName, MODE_PRIVATE);

MODE_PRIVATEFile creation mode: the mặc định mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
MODE_APPENDMode data appended khổng lồ the file if it already exists.

Xem thêm: Những Tác Dụng Sữa Chua Nếp Cẩm Dẫn Đến Phát Phì, Lợi Ích Khi Bà Bầu Ăn Sữa Chua Nếp Cẩm

MODE_WORLD_READABLEThese modes are very dangerous, it's lượt thích a security hole in Android, best not khổng lồ use, you can use alternative techniques such as:ContentProviderBroadcastReceiverService
MODE_MULTI_PROCESSThis mode allows multiple processes can be writen lớn the file. However, it is recommended that you should not use this mode because it does not work on some versions of Android. You can use other techniques:ContentProvider

// Is a simple tệp tin name.// Note!! vì not allow the path.String simpleFileName = "note.txt";// xuất hiện stream to read file.FileInputStream in = this.openFileInput(simpleFileName);
Now you can make an example of writing data lớn files stored in Internal Storage, and read data from this file.

Xem thêm: Bật Mí Cách Kiếm Tiền Qua Zalo Kiếm Tiền Bằng Cách Nào, Hướng Dẫn Các Cách Kiếm Tiền Zalo Pay Mới Nhất


File > New > New Project > Empty ActivityName: InternalStorageDemoPackage name: Java


The interface of this application is very simple, if you are interested in the steps lớn create it, see the appendix at the end of the article.
package;import android.os.Bundle;import;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import;import;import;import;public class MainActivity extends AppCompatActivity private Button saveButton; private Button readButton; private TextView textView; private EditText editText; // Is a simple tệp tin name. // Note!! vày not allow the path. Private String simpleFileName = "note.txt";
Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.saveButton = (Button) this.findViewById(; this.readButton = (Button) this.findViewById(; this.textView = (TextView) this.findViewById(; this.editText = (EditText) this.findViewById(; this.saveButton.setOnClickListener(new Button.OnClickListener()
Override public void onClick(View v) saveData(); ); this.readButton.setOnClickListener(new Button.OnClickListener()
Override public void onClick(View v) readData(); ); private void saveData() String data = this.editText.getText().toString(); try // xuất hiện Stream lớn write file. FileOutputStream out = this.openFileOutput(simpleFileName, MODE_PRIVATE); // Ghi dữ liệu. Out.write(data.getBytes()); out.close(); Toast.makeText(this,"File saved!",Toast.LENGTH_SHORT).show(); catch (Exception e) Toast.makeText(this,"Error:"+ e.getMessage(),Toast.LENGTH_SHORT).show(); private void readData() try // open stream to lớn read file. FileInputStream in = this.openFileInput(simpleFileName); BufferedReader br= new BufferedReader(new InputStreamReader(in)); StringBuilder sb= new StringBuilder(); String s= null; while((s= br.readLine())!= null) sb.append(s).append(" "); this.textView.setText(sb.toString()); catch (Exception e) Toast.makeText(this,"Error:"+ e.getMessage(),Toast.LENGTH_SHORT).show();