日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android文件存储教程,android开发基础教程—文件存储功能实现

發布時間:2025/5/22 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android文件存储教程,android开发基础教程—文件存储功能实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文件存儲:

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();

// 內部保存

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如果想要訪問這個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;

}

}

/**

* 通過文件絕對路徑讀取遠程文件

* @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);

}

/**

* 通過文件絕對路徑讀取遠程文件

* @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!");

}

/**

* 通過包相關上下文寫入遠程文件

* @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));

}

/**

* 通過包相關上下文寫入遠程文件

*/

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;

}

}

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的android文件存储教程,android开发基础教程—文件存储功能实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。