歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android的文件操作

Android的文件操作

日期:2017/3/1 10:55:40   编辑:Linux編程

目前,機會搜有的設備都會涉及到文件的操作,例如什麼電腦,手機等設備。Android的文件操作和電腦是比較類似的,既可以存儲在手機內置的存儲器裡也可以是sd卡。在這次的博客裡主要介紹在手機內置存儲器裡的文件操作。

一.開發流程

(1)界面的設計

(2)設計android的業務層

(3)單元測試

(4)設置android的控制器層

二.開發步驟

(1)設計軟件界面

  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. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/filename"
  11. />
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/filename"
  16. />
  17. <TextView
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:text="@string/content"
  21. />
  22. <EditText
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:id="@+id/content"
  26. android:minLines="3"
  27. />
  28. <Button
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="@string/button"
  32. android:id="@+id/button"/>
  33. </LinearLayout>
這裡也把R文件給大家看看
  1. /* AUTO-GENERATED FILE. DO NOT MODIFY.
  2. *
  3. * This class was automatically generated by the
  4. * aapt tool from the resource data it found. It
  5. * should not be modified by hand.
  6. */
  7. package org.lxh.file;
  8. public final class R {
  9. public static final class attr {
  10. }
  11. public static final class drawable {
  12. public static final int icon=0x7f020000;
  13. }
  14. public static final class id {
  15. public static final int button=0x7f050002;
  16. public static final int content=0x7f050001;
  17. public static final int filename=0x7f050000;
  18. }
  19. public static final class layout {
  20. public static final int main=0x7f030000;
  21. }
  22. public static final class string {
  23. public static final int app_name=0x7f040001;
  24. public static final int button=0x7f040004;
  25. public static final int content=0x7f040003;
  26. public static final int failure=0x7f040006;
  27. public static final int filename=0x7f040002;
  28. public static final int hello=0x7f040000;
  29. public static final int success=0x7f040005;
  30. }
  31. }
(2)設計業務層
  1. package org.lxh.service;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import android.content.Context;
  6. import android.util.Log;
  7. public class FileService {
  8. private Context context;
  9. public FileService(Context context) { //通過構造方法傳入context
  10. this.context = context;
  11. }
  12. //保存文件
  13. public void saveFile(String filename,String content) throws Exception{ //異常交給調用處處理
  14. FileOutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);
  15. out.write(content.getBytes());
  16. out.close();
  17. }
  18. public String readFile(String filename) throws Exception{ //異常交給調用處處理
  19. FileInputStream in=context.openFileInput(filename);
  20. byte b[]=new byte[1024];
  21. int len=0;
  22. ByteArrayOutputStream array=new ByteArrayOutputStream();
  23. while((len=in.read(b))!=-1){ //開始讀取文件
  24. array.write(b,0,len);
  25. }
  26. byte data[]=array.toByteArray(); //把內存裡的數據讀取出來
  27. in.close(); //每個流都必須關閉
  28. array.close();
  29. return new String(data); //把byte數組轉換為字符串並返回
  30. }
  31. }
Copyright © Linux教程網 All Rights Reserved