Android 文件存储--内部存储的例子

发布于 2022-09-30 19:51:02 字数 11399 浏览 17 评论 0

1)新建Android 项目,项目名称:DemoInternalStorage
2) 在继承于Activity的类中编写相应代码,代码如下所示:
  1. /*
  2. * Copyright (C) Mesada Technologies Co., Ltd. 2005-2010.
  3. * All rights reserved.
  4. *
  5. * This software is the confidential and proprietary information
  6. * of Mesada Technologies Co., Ltd. ("Confidential Information").
  7. * You shall not disclose such Confidential Information and shall
  8. * use it only in accordance with the terms of the license agreement
  9. * you entered into with Mesada.
  10. */
  11. package com.mesada.demo;

  12. import java.io.ByteArrayOutputStream;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;

  16. import android.app.Activity;
  17. import android.content.Context;
  18. import android.os.Bundle;
  19. import android.text.Editable;
  20. import android.text.TextWatcher;
  21. import android.util.Log;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.widget.Button;
  25. import android.widget.EditText;
  26. import android.widget.Toast;

  27. /**
  28. * This is a demo about file storage.
  29. *
  30. * @author Xiaolong Long
  31. * @date 2010-12-30
  32. * @version 1.0
  33. */
  34. public class MainActivity extends Activity implements OnClickListener {
  35. private static final String TAG = "MainActivity";
  36. private static final boolean mIsPrintInfo = true;

  37. private static final String FILENAME = "temp.txt";

  38. EditText mMsgView;
  39. Button mSave;
  40. Button mPrint;
  41. Button mCancel;

  42. boolean mIsLegal = false;

  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. if (mIsPrintInfo)
  46. Log.i(TAG, "onCreate()...");

  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.main);
  49. setupControlers();

  50. mSave.setEnabled(false);
  51. MsgTextWater msgTextWater = new MsgTextWater();
  52. mMsgView.addTextChangedListener(msgTextWater);

  53. mSave.setOnClickListener(this);
  54. mPrint.setOnClickListener(this);
  55. mCancel.setOnClickListener(this);
  56. }

  57. /**
  58. *
  59. * Find the views that were identified by the id attributes from the XML.
  60. *
  61. * @param
  62. * @return
  63. * @date 2010-12-30
  64. * @author Xiaolong Long
  65. */
  66. private void setupControlers() {
  67. if (mIsPrintInfo)
  68. Log.i(TAG, "setupControlers()...");

  69. mMsgView = (EditText) findViewById(R.id.msg);
  70. mSave = (Button) findViewById(R.id.saveMsg);
  71. mPrint = (Button) findViewById(R.id.printMsg);
  72. mCancel = (Button) findViewById(R.id.cancel);
  73. }


  74. /**
  75. *
  76. * Find the views that were identified by the id attributes from the XML.
  77. *
  78. * @param
  79. * @return
  80. * @date 2010-12-30
  81. * @author Xiaolong Long
  82. */
  83. private void setupControlers() {
  84. if (mIsPrintInfo)
  85. Log.i(TAG, "setupControlers()...");

  86. mMsgView = (EditText) findViewById(R.id.msg);
  87. mSave = (Button) findViewById(R.id.saveMsg);
  88. mPrint = (Button) findViewById(R.id.printMsg);
  89. mCancel = (Button) findViewById(R.id.cancel);
  90. }

  91. public void onClick(View v) {
  92. if (mIsPrintInfo)
  93. Log.i(TAG, "onClick()...");

  94. // Returns this view's identifier.
  95. int id = v.getId();
  96. switch (id) {
  97. case R.id.saveMsg:
  98. try {
  99. saveMsg();
  100. Toast.makeText(MainActivity.this, R.string.success_write,
  101. Toast.LENGTH_SHORT).show();
  102. mMsgView.setText("");
  103. } catch (IOException e) {
  104. Log.e(TAG,
  105. "failed to save the content to the file which called temp.txt",
  106. e);
  107. Toast.makeText(MainActivity.this, R.string.failed_write,
  108. Toast.LENGTH_SHORT).show();
  109. }
  110. break;
  111. case R.id.printMsg:
  112. try {
  113. mMsgView.requestFocus();
  114. mMsgView.setText(getMsg());
  115. } catch (IOException e) {
  116. Log.e(TAG,
  117. "failed to read a file from internal storage which called temp.txt",
  118. e);
  119. Toast.makeText(MainActivity.this, R.string.failed_read,
  120. Toast.LENGTH_SHORT).show();
  121. }
  122. break;
  123. case R.id.cancel:
  124. finish();
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. /**
  131. *
  132. * To create and write a file to the internal storage.
  133. *
  134. * @param
  135. * @return
  136. * @date 2010-12-30
  137. * @author Xiaolong Long
  138. */
  139. private void saveMsg() throws IOException {
  140. String msg = String.valueOf(mMsgView.getText());
  141. FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
  142. // To create and write a private file to the internal storage:
  143. fos.write(msg.getBytes("utf-8"));
  144. fos.flush();
  145. fos.close();
  146. }

  147. /**
  148. * To read a file from internal storage.
  149. *
  150. * @param
  151. * @return
  152. * @date 2010-12-30
  153. * @author Xiaolong Long
  154. */
  155. private String getMsg() throws IOException {
  156. FileInputStream fis = openFileInput(FILENAME);
  157. int length = FILENAME.length();
  158. byte[] buffer = new byte[length];

  159. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  160. int count = 0;
  161. while ((count = fis.read(buffer)) != -1) {
  162. bos.write(buffer, 0, count);
  163. }
  164. fis.close();
  165. bos.close();
  166. return bos.toString();
  167. }
  168. class MsgTextWater implements TextWatcher {

  169. public void afterTextChanged(Editable s) {

  170. }

  171. public void beforeTextChanged(CharSequence s, int start, int count,
  172. int after) {
  173. }

  174. public void onTextChanged(CharSequence s, int start, int before,
  175. int count) {
  176. mIsLegal = validate(s);
  177. if (mIsLegal) {
  178. mSave.setEnabled(true);
  179. return;
  180. }
  181. mSave.setEnabled(false);
  182. }

  183. /**
  184. *
  185. * To check the view if legal.
  186. *
  187. * @param
  188. * @return
  189. * @date 2010-12-30
  190. * @author Xiaolong Long
  191. */
  192. private boolean validate(CharSequence s) {
  193. String ss = s.toString();
  194. if (!"".equals(ss)) {
  195. return true;
  196. }
  197. return false;
  198. }
  199. }
  200. }
  201. 3)main.xml 文件如下所示:
  202. <?xml version="1.0" encoding="utf-8"?>
  203. <LinearLayout
  204. xmlns:android="http://schemas.android.com/apk/res/android"
  205. android:orientation="vertical"
  206. android:layout_width="fill_parent"
  207. android:layout_height="fill_parent">
  208. <TextView
  209. android:layout_width="fill_parent"
  210. android:layout_height="wrap_content"
  211. android:text="@string/enter_msg" />
  212. <EditText
  213. android:id="@+id/msg"
  214. android:layout_width="fill_parent"
  215. android:layout_height="wrap_content"></EditText>
  216. <LinearLayout
  217. android:layout_width="fill_parent"
  218. android:layout_height="wrap_content"
  219. android:orientation="horizontal"
  220. android:gravity="right">
  221. <Button
  222. android:id="@+id/saveMsg"
  223. android:text="@string/savemsg"
  224. android:layout_width="180px"
  225. android:layout_height="wrap_content"></Button>
  226. <Button
  227. android:id="@+id/printMsg"
  228. android:text="@string/printmsg"
  229. android:layout_width="145px"
  230. android:layout_height="wrap_content"></Button>
  231. <Button
  232. android:id="@+id/cancel"
  233. android:text="@string/cancel"
  234. android:layout_width="145px"
  235. android:layout_height="wrap_content"></Button>
  236. </LinearLayout>
  237. </LinearLayout>

  238. 4)AndroidMainfest.xml 文件如下所示:
  239. <?xml version="1.0" encoding="utf-8"?>
  240. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  241.       package="com.mesada.demo"
  242.       android:versionCode="1"
  243.       android:versionName="1.0">
  244.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  245.         <activity android:name=".MainActivity"
  246.                   android:label="@string/app_name">
  247.             <intent-filter>
  248.                 <action android:name="android.intent.action.MAIN" />
  249.                 <category android:name="android.intent.category.LAUNCHER" />
  250.             </intent-filter>
  251.         </activity>

  252.     </application>
  253.     <uses-sdk android:minSdkVersion="8" />

  254. </manifest>
复制代码5)完成,顺便上传截图:

1.jpg (47.43 KB, 下载次数: 2)

下载附件

2011-03-21 20:18 上传

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文