Computer >> Máy Tính >  >> Lập trình >> Android

Làm cách nào để tạo tệp txt trong bộ nhớ trong của Android?

Ví dụ này trình bày Cách tạo tệp txt trong bộ nhớ trong của android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/enterText"
      android:hint = "Please enter text here"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
   <Button
      android:id = "@+id/save"
      android:text = "Save"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã sử dụng editext và button. Khi người dùng nhấp vào nút, nó sẽ lấy dữ liệu từ việc chỉnh sửa văn bản và lưu trữ trong bộ nhớ trong dưới dạng /data/data//files/text/sample.txt.

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileWriter;
public class MainActivity extends AppCompatActivity {
   Button save;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final EditText enterText = findViewById(R.id.enterText);
      save = findViewById(R.id.save);
      save.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (!enterText.getText().toString().isEmpty()) {
               File file = new File(MainActivity.this.getFilesDir(), "text");
               if (!file.exists()) {
                  file.mkdir();
               }
               try {
                  File gpxfile = new File(file, "sample");
                  FileWriter writer = new FileWriter(gpxfile);
                  writer.append(enterText.getText().toString());
                  writer.flush();
                  writer.close();
                  Toast.makeText(MainActivity.this, "Saved your text", Toast.LENGTH_LONG).show();
               } catch (Exception e) { }
            }
         }
      });
   }
}

Bước 4 - Thêm mã sau vào tệp kê khai.xml

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
   package = "com.example.andy.myapplication">
   <uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ studio android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm cách nào để tạo tệp txt trong bộ nhớ trong của Android?

Trong kết quả trên, chúng tôi đã thêm một số văn bản và nhấp vào nút lưu như hình dưới đây -

Làm cách nào để tạo tệp txt trong bộ nhớ trong của Android?

Để xác minh kết quả trên, hãy /data/data//files/text/sample.txt như được hiển thị bên dưới -

Làm cách nào để tạo tệp txt trong bộ nhớ trong của Android?