android開發基礎教程—檔案儲存體功能實現_Android

來源:互聯網
上載者:User
檔案儲存體:
複製代碼 代碼如下:

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;
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.