- 文件
- SharedPreferences
- SQLite数据库
- 内容提供者(Content provider)(对外共享数据使用)
- 网络
本文讲讲文件的例子,其余后续讲解。
例子采用MVC模式
1业务bean(junit test) 2设计界面(美工) 这两项工作可以同步进行
3设计activity
android 内部类 为了性能,尽量少用类,合理。
例子
运行后截图
目录结构:
代码:
DataActivity.javapackage com.leo.file; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.leo.service.FileService; public class DataActivity extends Activity { private static final String TAG = "DataActivity"; private EditText filenameText; private EditText contentText; private TextView resultView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); filenameText = (EditText)this.findViewById(R.id.filename); contentText = (EditText)this.findViewById(R.id.content); resultView = (TextView)this.findViewById(R.id.result); Button button = (Button)this.findViewById(R.id.button); Button showButton = (Button)this.findViewById(R.id.showButton); button.setOnClickListener(listner); showButton.setOnClickListener(listner); } private View.OnClickListener listner = new View.OnClickListener() { public void onClick(View v) { Button button = (Button)v; String filename = filenameText.getText().toString(); switch (button.getId()){ case R.id.button: int resId = R.string.success; String content = contentText.getText().toString(); try { OutputStream outStream = DataActivity.this.openFileOutput(filename, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE); FileService.save(outStream, content); } catch (IOException e) { Log.e(TAG, e.toString()); resId = R.string.error; } Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG).show(); break; case R.id.showButton: try { InputStream inStream = DataActivity.this.openFileInput(filename); String text = FileService.read(inStream); resultView.setText(text); } catch (IOException e) { Log.e(TAG, e.toString()); Toast.makeText(DataActivity.this, "read error", Toast.LENGTH_LONG).show(); } break; } } }; }
FileServiceTest.javapackage com.leo.file; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.test.AndroidTestCase; import android.util.Log; import com.leo.service.FileService; public class FileServiceTest extends AndroidTestCase { private static final String TAG = "FileServiceTest"; public void testSave() throws Exception { OutputStream outStream = this.getContext().openFileOutput("leo.txt", Context.MODE_PRIVATE); FileService.save(outStream, "leo"); } public void testread() throws Exception { InputStream inStream = this.getContext().openFileInput("leo.txt"); String content = FileService.read(inStream); Log.i(TAG, content); } }
FileService.javapackage com.leo.service; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileService { /** * 保存数据 * @param outStream * @param content * @throws IOException */ public static void save (OutputStream outStream, String content) throws IOException { outStream.write(content.getBytes()); outStream.close(); } /** * 读取数据 * @param inStream * @return * @throws IOException */ public static String read (InputStream inStream) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while( (len = inStream.read(buffer)) != -1 ) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return new String (data); } }
strings.xml<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, DataActivity!</string> <string name="app_name">数据保存</string> <string name="filename">文件名称</string> <string name="content">文件内容</string> <string name="save">保存</string> <string name="show">显示文件内容</string> <string name="error">保存失败</string> <string name="success">保存成功</string> </resources>
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.leo.file" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> <activity android:name=".DataActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.leo.file" android:label="Tests for My App" /> </manifest>