如何在Android中将列表存储到txt文件以及从txt文件读取列表?
此示例演示了如何在Android中将列表存储到txt文件以及从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/<your.package.name>/files/text/sample.txt。它将从sample.txt追加列表数据到TextView。
步骤3 - 将以下代码添加到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.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
Button save;
ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<>();
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) {
list.add(line);
text.append(line);
text.append('
');
}
br.close();
} catch (IOException e) { }
String result = Arrays.toString(new ArrayList[]{list});
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运行应用程序,请打开项目中的一个活动文件,然后点击工具栏中的运行
图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -

在上面的结果中,我们添加了一些文本并点击了保存按钮,如下所示 -

要验证以上结果,请查看/data/data/<your.package.name>/files/text/sample.txt,如下所示 -

点击此处下载项目代码
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP