如何在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