a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

android開發(fā)基礎(chǔ)教程―文件存儲功能實現(xiàn)

2020-04-11 12:35:44
字體:
供稿:網(wǎng)友
文件存儲:
復(fù)制代碼 代碼如下:

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如果想要訪問這個mytext.txt文件格式如下:
復(fù)制代碼 代碼如下:

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!");
}
/**
* 通過包相關(guā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ā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;
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 午夜高清免费视频 | 色视频网址 | 精品免费视频 | 亚洲精选国产 | 亚洲毛片网站 | 国产精品久久久久久一级毛片 | 中文日韩在线 | 中文字幕国产一区 | 欧美 日韩 高清 | 精品久久久久久久久久久久久久 | 精品久久久久久久久久久久 | 成人精品一区二区三区中文字幕 | 日本激情视频在线播放 | 日本午夜精品 | 最新免费av网站 | 欧美日韩一区精品 | 国产精品欧美三级在线观看 | 日韩和的一区二区 | 99久久免费观看 | 久久久久毛片 | 日韩成人一区二区 | 免费视频一区 | 久久99国产精品 | 久久久久久久精 | 三级av在线 | 亚洲 自拍 另类 欧美 丝袜 | 国产精品99久久免费观看 | 黄网在线免费观看 | 在线看黄色片 | 瑟瑟网站在线观看 | 欧美中文字幕在线 | 91精品国产综合久久蜜臀 | 五月婷婷亚洲 | 色综合天天天天做夜夜夜夜做 | 国产精品美女视频 | 成人在线视频观看 | xxxx午夜| 国产精品福利网站 | 日韩成人在线播放 | 国产美女在线精品免费观看网址 | 日韩午夜激情 |