android開發(fā)基礎(chǔ)教程—文件存儲(chǔ)功能實(shí)現(xiàn)
public class MainActivity extends Activity {
EditText mname, mage;
TextView mtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mname = (EditText) findViewById(R.id.editText1);
mage = (EditText) findViewById(R.id.editText2);
mtv = (TextView) findViewById(R.id.textView1);
}
public void onClick(View v) {
String name = mname.getText().toString();
int age = Integer.parseInt(mage.getText().toString());
String cont = "name=" + name + ",age=" + age + "\n";
try {
int id = v.getId();
// 內(nèi)部保存
if (id == R.id.button1) {
FileOutputStream fos = this.openFileOutput("mytext.txt",
Context.MODE_APPEND | Context.MODE_WORLD_WRITEABLE
| Context.MODE_WORLD_READABLE);
fos.write(cont.getBytes());
fos.close();
Toast.makeText(this, "寫入完成", 1).show();
}
// 讀取
else if (id == R.id.button2) {
FileInputStream fis = this.openFileInput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
String str = new String(bytes);
mtv.setText(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
其他app如果想要訪問(wèn)這個(gè)mytext.txt文件格式如下:
public class MainActivity extends Activity {
TextView mcontent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontent=(TextView) findViewById(R.id.textView1);
}
public void onClick(View v){
switch (v.getId()) {
case R.id.button1:
try {
readRemoteFileByAbslutePath();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.button2:
try {
WriteRemoteFileByAbslutePath();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
/**
* 通過(guò)文件絕對(duì)路徑讀取遠(yuǎn)程文件
* @throws Exception
*/
public void readRemoteFileByAbslutePath() throws Exception{
String path = "/data/data/com.nanguabing.filedemo/files/mytext.txt" ;
FileInputStream fis = new FileInputStream(path);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
String str = new String(bytes);
mcontent.setText(str);
Log.i("Other", str);
}
/**
* 通過(guò)文件絕對(duì)路徑讀取遠(yuǎn)程文件
* @throws Exception
*/
public void WriteRemoteFileByAbslutePath() throws Exception{
String path = "/data/data/com.nanguabing.filedemo/files/mytext.txt" ;
FileOutputStream fos = new FileOutputStream(path,true);
fos.write("other write! ".getBytes());
fos.close();
Log.i("Other", "other write over!");
}
/**
* 通過(guò)包相關(guān)上下文寫入遠(yuǎn)程文件
* @throws Exception
*/
public void readRomoteByPackageContext() throws Exception {
String pname = "com.nanguabing.filedemo";
Context ctx = this.createPackageContext(pname,
Context.CONTEXT_IGNORE_SECURITY);
FileInputStream fis = ctx.openFileInput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
Log.i("Other",new String(bytes));
}
/**
* 通過(guò)包相關(guān)上下文寫入遠(yuǎn)程文件
*/
public void readRomoteByPackageContext2() throws Exception {
String pname = "com.nanguabing.filedemo";
Context ctx = this.createPackageContext(pname,
Context.CONTEXT_INCLUDE_CODE);
FileInputStream fis = ctx.openFileInput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
Log.i("Other",new String(bytes));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
- 詳解Android數(shù)據(jù)存儲(chǔ)之Android 6.0運(yùn)行時(shí)權(quán)限下文件存儲(chǔ)的思考
- Android實(shí)現(xiàn)文件存儲(chǔ)并讀取的示例代碼
- android數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)方法
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- 實(shí)例詳解Android文件存儲(chǔ)數(shù)據(jù)方式
- 詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(chǔ)(附源碼)
- Android學(xué)習(xí)之文件存儲(chǔ)讀取
- 詳解Android文件存儲(chǔ)
- Android編程之SharedPreferences文件存儲(chǔ)操作實(shí)例分析
- Android開發(fā)文件存儲(chǔ)實(shí)例
相關(guān)文章
Android多點(diǎn)觸控實(shí)現(xiàn)圖片自由縮放
這篇文章主要為大家詳細(xì)介紹了Android多點(diǎn)觸控實(shí)現(xiàn)圖片自由縮放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
淺談Android系統(tǒng)的基本體系結(jié)構(gòu)與內(nèi)存管理優(yōu)化
這篇文章主要介紹了Android系統(tǒng)的基本體系結(jié)構(gòu)與內(nèi)存管理優(yōu)化,非常簡(jiǎn)潔明了地總結(jié)了系統(tǒng)服務(wù)及垃圾回收等安卓的一些主要特性,需要的朋友可以參考下2016-02-02
flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Android自定義view之太極圖的實(shí)現(xiàn)教程
這篇文章主要給大家介紹了關(guān)于Android自定義view之太極圖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Android中顏色選擇器和改變字體顏色的實(shí)例教程
這篇文章主要介紹了Android中顏色選擇器和改變字體顏色的實(shí)例教程,其中改變字體顏色用到了ColorPicker顏色選擇器,需要的朋友可以參考下2016-04-04
Android序列化接口Parcelable與Serializable接口對(duì)比
我們使用 Intent 傳遞數(shù)據(jù)的時(shí)候,putExtra() 所支持的數(shù)據(jù)類型事有限的,當(dāng)需要傳遞自定義對(duì)象的時(shí)候就需要序列化。Serializable更簡(jiǎn)單但是會(huì)把整個(gè)對(duì)象進(jìn)行序列化因此效率比Parcelable低一些2023-02-02
c++ mk文件出錯(cuò)Jni調(diào)用產(chǎn)生java.lang.UnsatisfiedLinkError錯(cuò)誤解決方法
錯(cuò)誤產(chǎn)生在我把方法從c語(yǔ)言轉(zhuǎn)為c++語(yǔ)言后產(chǎn)生的,后來(lái)檢查到這種錯(cuò)誤是因?yàn)閙k文件出錯(cuò),加載c文件和加載c++的文件所用的代碼不一樣,下面請(qǐng)看2013-11-11
ListView實(shí)現(xiàn)下拉刷新加載更多的實(shí)例代碼(直接拿來(lái)用)
這篇文章主要介紹了ListView實(shí)現(xiàn)下拉刷新加載更多的實(shí)例代碼(直接拿來(lái)用)的相關(guān)資料,需要的朋友可以參考下2016-07-07
淺談android Fragment橫豎屏翻轉(zhuǎn)對(duì)重新加載的要求
下面小編就為大家分享一篇淺談android Fragment橫豎屏翻轉(zhuǎn)對(duì)重新加載的要求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

