android app 內部檔案路徑

來源:互聯網
上載者:User

標籤:

 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             @Override17             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             @Override26             public void onClick(View v) {27                 et2.setText(read());28             }29         });30 31     }32 33     /**34      * 寫入到/data/data/[包名]/files 路徑下35      * 36      * @param content37      */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      * @return54      */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 }

 

android app 內部檔案路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.