android app 内部文件路径
1 public class MainActivity extends Activity { 2 3 final String FILE_NAME = "crazyit.bin"; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 10 final EditText et1 = (EditText) findViewById(R.id.edt1); 11 final EditText et2 = (EditText) findViewById(R.id.edt2); 12 13 Button btnWrite = (Button) findViewById(R.id.btnIn); 14 btnWrite.setOnClickListener(new OnClickListener() { 15 16 @Override 17 public void onClick(View v) { 18 write(et1.getText().toString()); 19 et1.setText(""); 20 } 21 }); 22 Button btnRead = (Button) findViewById(R.id.btnOut); 23 btnRead.setOnClickListener(new OnClickListener() { 24 25 @Override 26 public void onClick(View v) { 27 et2.setText(read()); 28 } 29 }); 30 31 } 32 33 /** 34 * 写入到/data/data/[包名]/files 路径下 35 * 36 * @param content 37 */ 38 private void write(String content) { 39 try { 40 FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_APPEND); 41 PrintStream ps = new PrintStream(fos); 42 ps.println(content); 43 ps.close(); 44 45 } catch (FileNotFoundException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 /** 51 * /data/data/[包名]/files 路径下读取文件 52 * 53 * @return 54 */ 55 private String read() { 56 try { 57 58 FileInputStream fis = openFileInput(FILE_NAME); 59 byte[] buff = new byte[1024]; 60 int hasRead = 0; 61 StringBuilder sb = new StringBuilder(""); 62 while ((hasRead = fis.read(buff)) > 0) { 63 sb.append(new String(buff, 0, hasRead)); 64 } 65 fis.close(); 66 return sb.toString(); 67 68 } catch (FileNotFoundException e) { 69 e.printStackTrace(); 70 } catch (IOException e) { 71 e.printStackTrace(); 72 } 73 74 return null; 75 } 76 77 }