android開(kāi)發(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, "寫(xiě)入完成", 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)上下文寫(xiě)入遠(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)上下文寫(xiě)入遠(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開(kāi)發(fā)數(shù)據(jù)持久化之文件存儲(chǔ)(附源碼)
- Android學(xué)習(xí)之文件存儲(chǔ)讀取
- 詳解Android文件存儲(chǔ)
- Android編程之SharedPreferences文件存儲(chǔ)操作實(shí)例分析
- Android開(kāi)發(fā)文件存儲(chǔ)實(shí)例
相關(guān)文章
詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android中FileProvider的各種場(chǎng)景應(yīng)用詳解
這篇文章主要為大家介紹了Android中FileProvider的各種場(chǎng)景應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Flutter如何完成路由攔截,實(shí)現(xiàn)權(quán)限管理
本篇介紹了利用 Fluro 路由管理實(shí)現(xiàn)路由權(quán)限攔截的兩種方式,兩種方式各有好處,使用過(guò)程中可以根據(jù)實(shí)際情況決定使用哪一種方法。2021-06-06Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法,結(jié)合實(shí)例形式分析了Android遠(yuǎn)程播放音頻文件的相關(guān)步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08解析Android中如何做到Service被關(guān)閉后又自動(dòng)啟動(dòng)的實(shí)現(xiàn)方法
本篇文章是對(duì)在Android中如何做到Service被關(guān)閉后又自動(dòng)啟動(dòng)的方法進(jìn)行了詳細(xì)的分析和介紹。需要的朋友參考下2013-05-05ERROR/AndroidRuntime(17121)的問(wèn)題解決
ERROR/AndroidRuntime(17121)的問(wèn)題解決,需要的朋友可以參考一下2013-05-05android開(kāi)發(fā)教程之使用線程實(shí)現(xiàn)視圖平滑滾動(dòng)示例
這篇文章主要介紹了android使用線程實(shí)現(xiàn)視圖平滑滾動(dòng)示例,需要的朋友可以參考下2014-03-03Android Activity的跳轉(zhuǎn)與傳值詳解
這篇文章主要介紹了Android Activity的跳轉(zhuǎn)與傳值詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06