寫入讀取類型分了好幾種
1.SharedPreferences
以key-value方式儲存只有Private data
2.Internal Storage
使用device內部的記憶體儲存Private data
3.External Storage
使用如SD卡這樣的外部儲存媒介,儲存Public data
4.SQLite Databases
以資料庫結構儲存private data
5.Network Connection
儲存資料到網路主機上
相關介紹 參考
Android 資料儲存
http://ozzysun.blogspot.tw/2010/11/android.html
Manifest File
加入這兩行,開取存讀取的權限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Layout
注意 TextView 內的 android:scrollbars(垂直滾動條),android:singleLine(實現多行),這關於TextView是否可以被滑動滾輪。
除Layout中,Main也需要追加一行code,來實現被拖動/滾動的動作。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:id="@+id/frameLayout"
android:layout_below="@+id/button2"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:scrollbars="vertical"
android:singleLine="false"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="207dp" />
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="@+id/button"
android:layout_marginTop="63dp"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"
android:id="@+id/button2"
android:layout_gravity="right|center_vertical"
android:layout_alignTop="@+id/button"
android:layout_alignParentEnd="true"
android:layout_marginEnd="38dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Main
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private EditText EditText;
private Button saveButton;
private Button readButton;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText = (EditText) findViewById(R.id.editText);
saveButton = (Button) findViewById(R.id.button);
readButton = (Button) findViewById(R.id.button2);
textView = (TextView) findViewById(R.id.textView);
//追加滾動/滑動的動作指令
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
saveButton.setOnClickListener(saveClickListener);
readButton.setOnClickListener(readClickListener);
}
//按下Save按鈕
private Button.OnClickListener saveClickListener = new Button.OnClickListener() {
public void onClick(View arg0) {
String filename = "CheckFlie.txt";
// 存放檔案位置在 內部空間/Download/
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, filename);
try
{
// 第二個參數為是否 append
// 若為 true,則新加入的文字會接續寫在文字檔的最後
FileOutputStream Output = new FileOutputStream(file, true);
String dateformat = "yyyyMMdd kk:mm:ss";
SimpleDateFormat df = new SimpleDateFormat(dateformat);
df.applyPattern(dateformat);
String string = df.format(new Date()) + " : " + EditText.getText() + "\n";
Output.write(string.getBytes());
Output.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
};
//按下Read按鈕
private Button.OnClickListener readClickListener = new Button.OnClickListener() {
public void onClick(View arg0) {
String filename = "CheckFlie.txt";
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, filename);
try{
//建立FileReader物件,並設定讀取的檔案為CheckFlie.txt
FileReader fr = new FileReader(file);
//將BufferedReader與FileReader做連結
BufferedReader bufFile = new BufferedReader(fr);
String readData = "";
String temp = bufFile.readLine(); //readLine()讀取一整行
while (temp!=null){
readData+=temp + "\n";
temp=bufFile.readLine();
}
textView.setText(readData);
}catch(Exception e){
e.printStackTrace();
}
}
};
}
//--------------------------------------------------------------------
附上Farmer code
https://drive.google.com/drive/folders/0B4Nz-XuKqGazQmZwRGUzLU15SUE?usp=sharing
//--------------------------------------------------------------------
參考資料
[Android] 讀取文字檔顯示在Toast
http://style77125tech.pixnet.net/blog/post/17737594
[Android] Stream方式寫入檔案
http://style77125tech.pixnet.net/blog/post/17636390
【Android Studio】寫入純文字檔
http://oldgrayduck.blogspot.tw/2015/11/android-studio_23.html
Android學習筆記 - 儲存偏好設定(Preference)
http://cooking-java.blogspot.tw/2010/04/android-preference.html
資料測試改寫,如有任何版權問題,請告知,立即刪除,謝謝
沒有留言:
張貼留言