如何在Android内部存储中创建和读取txt文件?
此示例演示如何在Android内部存储中创建和读取txt文件。
步骤1 - 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建一个新项目。
步骤2 - 将以下代码添加到res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://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" /> <TextView android:id = "@+id/output" android:layout_width = "wrap_content" android:textSize = "25sp" android:layout_height = "wrap_content" /> </LinearLayout>
在上面的代码中,我们使用了EditText和Button。当用户点击按钮时,它将从EditText获取数据并将其存储到内部存储中的/data/data/<你的包名>/files/text/sample.txt。它将从sample.txt追加数据到TextView。
步骤3 - 将以下代码添加到src/MainActivity.java
package com.example.andy.myapplication; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { Button save; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView output = findViewById(R.id.output); 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(); output.setText(readFile()); Toast.makeText(MainActivity.this, "Saved your text", Toast.LENGTH_LONG).show(); } catch (Exception e) { } } } }); } private String readFile() { File fileEvents = new File(MainActivity.this.getFilesDir()+"/text/sample"); StringBuilder text = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(fileEvents)); String line; while ((line = br.readLine()) ! = null) { text.append(line); text.append('
'); } br.close(); } catch (IOException e) { } String result = text.toString(); return result; } }
步骤4 - 将以下代码添加到manifest.xml
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication"> <uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name = "android.permission.READ_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>
让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开您的项目中的一个activity文件,然后单击工具栏中的运行 图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -
在上面的结果中,我们添加了一些文本并点击了保存按钮,如下所示 -
要验证以上结果,请查看/data/data/<你的包名>/files/text/sample.txt,如下所示 -
点击这里下载项目代码
广告