国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

android開(kāi)發(fā)中文件存儲(chǔ)的演示

2020-02-21 17:36:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

隨著移動(dòng)互聯(lián)網(wǎng)的發(fā)展,現(xiàn)在軟件開(kāi)發(fā)大部分都是在IOS和android系統(tǒng)中進(jìn)行開(kāi)發(fā),這也讓很多程序開(kāi)發(fā)人員非常感興趣,那么android開(kāi)發(fā)中文件存儲(chǔ)的演示大家了解嗎?下面就跟著武林技術(shù)頻道小編來(lái)了解吧!

android開(kāi)發(fā)中文件存儲(chǔ)的演示

文件存儲(chǔ):


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開(kāi)發(fā)中文件存儲(chǔ)的演示,感興趣的朋友跟著武林技術(shù)頻道小編來(lái)了解吧!
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 麻阳| 三门峡市| 余江县| 西贡区| 丽江市| 桂东县| 钟祥市| 大竹县| 桐梓县| 东山县| 鄂托克旗| 同德县| 郸城县| 平江县| 高邮市| 罗甸县| 诸暨市| 兴文县| 天祝| 云南省| 平谷区| 礼泉县| 龙江县| 黔西县| 额尔古纳市| 道孚县| 松江区| 怀宁县| 玉树县| 砀山县| 特克斯县| 登封市| 永兴县| 运城市| 泰顺县| 乳山市| 开封市| 那坡县| 周宁县| 池州市| 眉山市|