Android編程中File文件常見存儲與讀取操作demo示例
本文實(shí)例講述了Android編程中File文件常見存儲與讀取操作。分享給大家供大家參考,具體如下:
MainActivity文件代碼如下:
package example.com.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class MainActivity extends Activity
{
final String FILE_NAME = "test.txt";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(new StringBuilder("a").append("b").append("c")
.toString());
// 獲取兩個按鈕
Button read = (Button) findViewById(R.id.read);
Button write = (Button) findViewById(R.id.write);
// 獲取兩個文本框
final EditText edit1 = (EditText) findViewById(R.id.edit1);
final EditText edit2 = (EditText) findViewById(R.id.edit2);
// 為write按鈕綁定事件監(jiān)聽器
write.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View source)
{
// 將edit1中的內(nèi)容寫入文件中
write(edit1.getText().toString());
edit1.setText("");
}
});
read.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// 讀取指定文件中的內(nèi)容,并顯示出來
edit2.setText(read());
}
});
}
private String read()
{
try
{
// 打開文件輸入流
FileInputStream fis = openFileInput(FILE_NAME);
byte[] buff = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder("");
while ((hasRead = fis.read(buff)) > 0)
{
sb.append(new String(buff, 0, hasRead));
}
return sb.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private void write(String content)
{
try
{
// 以追加模式打開文件輸出流
FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND);
// 將FileOutputStream包裝成PrintStream
PrintStream ps = new PrintStream(fos);
// 輸出文件內(nèi)容
ps.println(content);
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
布局文件代碼如下:
<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"/>
<Button
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="write"/>
<EditText
android:id="@+id/edit2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:lines="4"/>
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="read"/>
</LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android開發(fā)實(shí)現(xiàn)文件存儲功能
- Android存儲字符串?dāng)?shù)據(jù)到txt文件
- Android圖片添加水印圖片并把圖片保存到文件存儲的實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫入存儲卡的方法
- Android用文件存儲數(shù)據(jù)的方法
- Android基礎(chǔ)教程數(shù)據(jù)存儲之文件存儲
- Android實(shí)現(xiàn)文件存儲并讀取的示例代碼
- Android 文件數(shù)據(jù)存儲實(shí)例詳解
- 詳解Android數(shù)據(jù)存儲之Android 6.0運(yùn)行時權(quán)限下文件存儲的思考
- android數(shù)據(jù)存儲之文件存儲方法
- 詳解Android 中的文件存儲
相關(guān)文章
關(guān)于Android Studio封裝SDK的那些事兒
這篇文章主要給大家介紹了關(guān)于Android Studio封裝SDK的那些事兒,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Android抓取CSDN首頁極客頭條內(nèi)容完整實(shí)例
這篇文章主要介紹了Android抓取CSDN首頁極客頭條內(nèi)容完整實(shí)例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
基于Android中手勢交互的實(shí)現(xiàn)方法
本篇文章是對Android中手勢交互的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
Android沉浸式頂部實(shí)現(xiàn)代碼及效果
這篇文章主要介紹了Android沉浸式頂部實(shí)現(xiàn)代碼及效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Android手勢密碼view學(xué)習(xí)筆記(一)
這篇文章主要為大家詳細(xì)介紹了Android手勢密碼view的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
詳解Android開發(fā)中Activity的四種launchMode
這篇文章主要介紹了Android開發(fā)中Activity的四種launchMode,launchMode主要用于控制多個Activity間的跳轉(zhuǎn),需要的朋友可以參考下2016-03-03
Android NavigationView頭部設(shè)置監(jiān)聽事件
這篇文章主要為大家詳細(xì)介紹了Android NavigationView頭部設(shè)置監(jiān)聽事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10

