当前位置: 代码迷 >> Android >> android-在目前应用的目录下创建一个文件
  详细解决方案

android-在目前应用的目录下创建一个文件

热度:26   发布时间:2016-04-28 06:00:34.0
android--在当前应用的目录下创建一个文件
/********************************************************************* * Author  : Samson * Date    : 04/21/2014 * Test platform: *              3.11.0-12-generic #19-Ubuntu *              GNU bash, version 4.2.45 * *******************************************************************/package com.example.listfile;import java.io.File;   import java.io.IOException;    import android.app.Activity;   import android.content.Context;import android.widget.TextView;import android.os.Bundle;     import android.os.Process;public class MainActivity extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        int myProcessID = Process.myPid();        File yygypath = this.getFilesDir();//this.getCacheDir();        String yygypathstr = yygypath.toString();        File file = new File(yygypath, "yygytest");         try {			file.createNewFile();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}        yygypathstr = yygypathstr + " pid is " + myProcessID;                TextView  tv = new TextView(this);        tv.setText(yygypathstr);        setContentView(tv);    }    public void onDestory()    {    	super.onDestroy();    	this.finish();    	android.os.Process.killProcess(android.os.Process.myPid());    	System.exit(0);    	    }}其中,比较重要的是
this.getFilesDir();  //得到当前执行程序目录下的files目录的路径this.getCacheDir();  //得到当前执行程序目录下的cache目录的路径//以下是在files目录下创建一个名为yygytest的文件
File file = new File(yygypath, "yygytest"); file.createNewFile();
 

执行结果 :

adb shell result:
 [email protected]:/data/data/com.example.listfile/files # ll                        
-rw------- u0_a53   u0_a53          0 2014-04-21 13:38 yygytest
[email protected]:/data/data/com.example.listfile/files #


done……………………
  相关解决方案