android文件读取
DataActivity.java
FileServiceTest.java ---单元测试模块
FileService.java ----javabean模块
main.xml ---布局文件
strings.xml
AndroidManifest.xml
1
package
com.pptv.file;
2
3 import java.io.FileNotFoundException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.os.Bundle;
10 import android.util.Log;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.EditText;
14 import android.widget.TextView;
15 import android.widget.Toast;
16
17 import com.pptv.service.FileService;
18
19 public class DataActivity extends Activity {
20 private EditText filenameText;
21 private EditText contentText;
22 private TextView resultView;
23 private static final String TAG = "DataActivity";
24
25 @Override
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.main);
29 filenameText = (EditText) findViewById(R.id.edittext_filename);
30 contentText = (EditText) findViewById(R.id.textconetent);
31 resultView = (TextView) findViewById(R.id.textview_result);
32 Button button = (Button) findViewById(R.id.button_save);
33 Button buttonShow = (Button) findViewById(R.id.button_open);
34 button.setOnClickListener(listener);
35 buttonShow.setOnClickListener(listener);
36 }
37
38 private View.OnClickListener listener = new View.OnClickListener() {
39
40 @Override
41 public void onClick(View v) {
42 Button button = (Button) v;
43 String filename = filenameText.getText().toString();
44 switch (button.getId()) {
45 case R.id.button_save:
46 int resId = R.string.sucess;
47 String content = contentText.getText().toString();
48 OutputStream outStream;
49 try {
50 outStream = DataActivity.this.openFileOutput(filename,
51 Context.MODE_APPEND);
52 FileService.save(outStream, content);
53 } catch (Exception e) {
54 Log.e(TAG, e.toString());
55 resId = R.string.error;
56 }
57 Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG)
58 .show();
59 break;
60
61 case R.id.button_open:
62 try {
63 InputStream inStream = DataActivity.this
64 .openFileInput(filename);
65 String text = FileService.read(inStream, null);
66 resultView.setText(text);
67 } catch (Exception e) {
68 Log.e(TAG, e.toString());
69 Toast.makeText(DataActivity.this, "读取失败", Toast.LENGTH_LONG).show();
70 }
71 break;
72
73 default:
74 break;
75 }
76
77 }
78 };
79
80}
81
2
3 import java.io.FileNotFoundException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.os.Bundle;
10 import android.util.Log;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.EditText;
14 import android.widget.TextView;
15 import android.widget.Toast;
16
17 import com.pptv.service.FileService;
18
19 public class DataActivity extends Activity {
20 private EditText filenameText;
21 private EditText contentText;
22 private TextView resultView;
23 private static final String TAG = "DataActivity";
24
25 @Override
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.main);
29 filenameText = (EditText) findViewById(R.id.edittext_filename);
30 contentText = (EditText) findViewById(R.id.textconetent);
31 resultView = (TextView) findViewById(R.id.textview_result);
32 Button button = (Button) findViewById(R.id.button_save);
33 Button buttonShow = (Button) findViewById(R.id.button_open);
34 button.setOnClickListener(listener);
35 buttonShow.setOnClickListener(listener);
36 }
37
38 private View.OnClickListener listener = new View.OnClickListener() {
39
40 @Override
41 public void onClick(View v) {
42 Button button = (Button) v;
43 String filename = filenameText.getText().toString();
44 switch (button.getId()) {
45 case R.id.button_save:
46 int resId = R.string.sucess;
47 String content = contentText.getText().toString();
48 OutputStream outStream;
49 try {
50 outStream = DataActivity.this.openFileOutput(filename,
51 Context.MODE_APPEND);
52 FileService.save(outStream, content);
53 } catch (Exception e) {
54 Log.e(TAG, e.toString());
55 resId = R.string.error;
56 }
57 Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG)
58 .show();
59 break;
60
61 case R.id.button_open:
62 try {
63 InputStream inStream = DataActivity.this
64 .openFileInput(filename);
65 String text = FileService.read(inStream, null);
66 resultView.setText(text);
67 } catch (Exception e) {
68 Log.e(TAG, e.toString());
69 Toast.makeText(DataActivity.this, "读取失败", Toast.LENGTH_LONG).show();
70 }
71 break;
72
73 default:
74 break;
75 }
76
77 }
78 };
79
80}
81
FileServiceTest.java ---单元测试模块
1
package
com.pptv.file;
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5
6 import android.content.Context;
7 import android.test.AndroidTestCase;
8 import android.util.Log;
9
10 import com.pptv.service.FileService;
11
12 public class FileServiceTest extends AndroidTestCase {
13 private static final String TAG = "FileServiceTest";
14
15 public void testSave() throws Exception {
16 OutputStream outStream = this.getContext().openFileOutput("songwei.txt", Context.MODE_PRIVATE);
17 FileService.save(outStream, "neironga!!!");
18 }
19
20 public void testRead() throws Exception {
21 InputStream inStream = this.getContext().openFileInput("songwei.txt");
22 String content = FileService.read(inStream, null);
23 Log.i(TAG, content);
24 }
25}
26
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5
6 import android.content.Context;
7 import android.test.AndroidTestCase;
8 import android.util.Log;
9
10 import com.pptv.service.FileService;
11
12 public class FileServiceTest extends AndroidTestCase {
13 private static final String TAG = "FileServiceTest";
14
15 public void testSave() throws Exception {
16 OutputStream outStream = this.getContext().openFileOutput("songwei.txt", Context.MODE_PRIVATE);
17 FileService.save(outStream, "neironga!!!");
18 }
19
20 public void testRead() throws Exception {
21 InputStream inStream = this.getContext().openFileInput("songwei.txt");
22 String content = FileService.read(inStream, null);
23 Log.i(TAG, content);
24 }
25}
26
FileService.java ----javabean模块
1
package
com.pptv.service;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 public class FileService {
8 /** *//**
9 * 保存数据~~
10 * @param outStream
11 * @param content
12 * @throws Exception
13 */
14 public static void save(OutputStream outStream, String content) throws Exception {
15 outStream.write(content.getBytes());
16 outStream.close();
17 }
18
19 /** *//**
20 * 读取数据~~
21 * @param inStream
22 * @param content
23 * @return
24 * @throws Exception
25 */
26 public static String read(InputStream inStream, String content) throws Exception {
27 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
28 byte[] buffer = new byte[1024];
29 int len = -1;
30 while ((len = inStream.read(buffer)) != -1) {
31 outStream.write(buffer, 0, len);
32 }
33 byte[] data = outStream.toByteArray();
34 outStream.close();
35 inStream.close();
36 return new String(data);
37 }
38}
39
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 public class FileService {
8 /** *//**
9 * 保存数据~~
10 * @param outStream
11 * @param content
12 * @throws Exception
13 */
14 public static void save(OutputStream outStream, String content) throws Exception {
15 outStream.write(content.getBytes());
16 outStream.close();
17 }
18
19 /** *//**
20 * 读取数据~~
21 * @param inStream
22 * @param content
23 * @return
24 * @throws Exception
25 */
26 public static String read(InputStream inStream, String content) throws Exception {
27 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
28 byte[] buffer = new byte[1024];
29 int len = -1;
30 while ((len = inStream.read(buffer)) != -1) {
31 outStream.write(buffer, 0, len);
32 }
33 byte[] data = outStream.toByteArray();
34 outStream.close();
35 inStream.close();
36 return new String(data);
37 }
38}
39
main.xml ---布局文件
1
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2 < LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
3 android:orientation = " vertical "
4 android:layout_width = " fill_parent "
5 android:layout_height = " fill_parent "
6 >
7 < RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android "
8 android:orientation = " vertical "
9 android:layout_width = " fill_parent "
10 android:layout_height = " wrap_content " >
11 < TextView
12 android:id = " @+id/filename "
13 android:layout_width = " wrap_content "
14 android:layout_height = " 10pt "
15
16 android:text = " @string/file_name "
17 />
18
19 < EditText
20 android:layout_width = " 100pt "
21 android:layout_height = " wrap_content "
22 android:layout_toRightOf = " @id/filename "
23 android:layout_marginLeft = " 10pt "
24 android:id = " @+id/edittext_filename "
25 />
26 </ RelativeLayout >
27
28 < TextView
29 android:layout_width = " wrap_content "
30 android:layout_height = " wrap_content "
31 android:text = " @string/txtcontent "
32 />
33 < EditText
34 android:id = " @+id/textconetent "
35 android:layout_width = " fill_parent "
36 android:layout_height = " wrap_content "
37 android:minLines = " 4 "
38 />
39
40 < RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android "
41 android:orientation = " vertical "
42 android:layout_width = " fill_parent "
43 android:layout_height = " wrap_content " >
44 < Button
45 android:id = " @+id/button_save "
46 android:layout_width = " wrap_content "
47 android:layout_height = " wrap_content "
48 android:text = " @string/button_save "
49 />
50 < Button
51 android:id = " @+id/button_open "
52 android:layout_width = " wrap_content "
53 android:layout_height = " wrap_content "
54 android:layout_toRightOf = " @id/button_save "
55 android:text = " @string/button_open "
56 />
57 </ RelativeLayout >
58
59 < TextView
60 android:id = " @+id/textview_result "
61 android:layout_width = " fill_parent "
62 android:layout_height = " wrap_content "
63 />
64
65 </ LinearLayout >
2 < LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
3 android:orientation = " vertical "
4 android:layout_width = " fill_parent "
5 android:layout_height = " fill_parent "
6 >
7 < RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android "
8 android:orientation = " vertical "
9 android:layout_width = " fill_parent "
10 android:layout_height = " wrap_content " >
11 < TextView
12 android:id = " @+id/filename "
13 android:layout_width = " wrap_content "
14 android:layout_height = " 10pt "
15
16 android:text = " @string/file_name "
17 />
18
19 < EditText
20 android:layout_width = " 100pt "
21 android:layout_height = " wrap_content "
22 android:layout_toRightOf = " @id/filename "
23 android:layout_marginLeft = " 10pt "
24 android:id = " @+id/edittext_filename "
25 />
26 </ RelativeLayout >
27
28 < TextView
29 android:layout_width = " wrap_content "
30 android:layout_height = " wrap_content "
31 android:text = " @string/txtcontent "
32 />
33 < EditText
34 android:id = " @+id/textconetent "
35 android:layout_width = " fill_parent "
36 android:layout_height = " wrap_content "
37 android:minLines = " 4 "
38 />
39
40 < RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android "
41 android:orientation = " vertical "
42 android:layout_width = " fill_parent "
43 android:layout_height = " wrap_content " >
44 < Button
45 android:id = " @+id/button_save "
46 android:layout_width = " wrap_content "
47 android:layout_height = " wrap_content "
48 android:text = " @string/button_save "
49 />
50 < Button
51 android:id = " @+id/button_open "
52 android:layout_width = " wrap_content "
53 android:layout_height = " wrap_content "
54 android:layout_toRightOf = " @id/button_save "
55 android:text = " @string/button_open "
56 />
57 </ RelativeLayout >
58
59 < TextView
60 android:id = " @+id/textview_result "
61 android:layout_width = " fill_parent "
62 android:layout_height = " wrap_content "
63 />
64
65 </ LinearLayout >
strings.xml
1
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2 < resources >
3 < string name = " txtcontent " > 文本内容: </ string >
4 < string name = " app_name " > 数据保存 </ string >
5 < string name = " file_name " > 文件名称: </ string >
6 < string name = " button_save " > 保 存 </ string >
7 < string name = " error " > 保存失败 </ string >
8 < string name = " sucess " > 保存成功 </ string >
9 < string name = " button_open " > 打开文件 </ string >
10 </ resources >
11
2 < resources >
3 < string name = " txtcontent " > 文本内容: </ string >
4 < string name = " app_name " > 数据保存 </ string >
5 < string name = " file_name " > 文件名称: </ string >
6 < string name = " button_save " > 保 存 </ string >
7 < string name = " error " > 保存失败 </ string >
8 < string name = " sucess " > 保存成功 </ string >
9 < string name = " button_open " > 打开文件 </ string >
10 </ resources >
11
AndroidManifest.xml
1
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2 < manifest xmlns:android = " http://schemas.android.com/apk/res/android "
3 package = " com.pptv.file "
4 android:versionCode = " 1 "
5 android:versionName = " 1.0 " >
6 < application android:icon = " @drawable/icon " android:label = " @string/app_name " >
7 // 测试权限
8 < uses - library android:name = " android.test.runner " />
9
10 < activity android:name = " .DataActivity "
11 android:label = " @string/app_name " >
12 < intent - filter >
13 < action android:name = " android.intent.action.MAIN " />
14 < category android:name = " android.intent.category.LAUNCHER " />
15 </ intent - filter >
16 </ activity >
17
18 </ application >
19 < uses - sdk android:minSdkVersion = " 4 " />
20 // 测试权限
21 < instrumentation android:name = " android.test.InstrumentationTestRunner "
22 android:targetPackage = " com.pptv.file " android:label = " Tests for My App " />
23 </ manifest >
2 < manifest xmlns:android = " http://schemas.android.com/apk/res/android "
3 package = " com.pptv.file "
4 android:versionCode = " 1 "
5 android:versionName = " 1.0 " >
6 < application android:icon = " @drawable/icon " android:label = " @string/app_name " >
7 // 测试权限
8 < uses - library android:name = " android.test.runner " />
9
10 < activity android:name = " .DataActivity "
11 android:label = " @string/app_name " >
12 < intent - filter >
13 < action android:name = " android.intent.action.MAIN " />
14 < category android:name = " android.intent.category.LAUNCHER " />
15 </ intent - filter >
16 </ activity >
17
18 </ application >
19 < uses - sdk android:minSdkVersion = " 4 " />
20 // 测试权限
21 < instrumentation android:name = " android.test.InstrumentationTestRunner "
22 android:targetPackage = " com.pptv.file " android:label = " Tests for My App " />
23 </ manifest >