1. 程式人生 > 其它 >學習日誌17

學習日誌17

技術標籤:android

學習目標:

資料儲存與訪問

學習內容:

1.檔案的相關操作方法

openFileOutput(filename,mode):開啟檔案輸出流
opeanFileIntput(filename):開啟檔案輸入流
getDir(name,mode):在app的data目錄下獲取或建立對應的子目錄
getFileDir():獲取app的data目錄的file目錄的絕對路徑
String[] FileList() 返回app的data目錄下的全部檔案
deleteFile(filename):刪除app的data目錄下的指定檔案

2.案例演示
1.首先是main_activity.xml

<LinearLayout xmlns:android = “ http://schemas.android.com/apk/res/android” xmlns:tools = “ http://schemas.android.com/tools” android:id = “ @ + id / LinearLayout1 “ android:layout_width = ” match_parent“ android:layout_height = ” match_parent“ android:orientation = ”垂直“工具:上下文= ” com.jay.example.filedemo1.
MainActivity“ > <TextView android:layout_width = “包裝內容” android:layout_height = “包裝內容” android:text = “ @ string / nametitle” /> <EditText android:id = “ @ + id / editname” android:layout_width = “ match_parent” android:layout_height =
“ wrap_content” /> <TextView android:layout_width = “包裝內容” android:layout_height = “包裝內容” android:text = “ @ string / detailtitle” /> <EditText android:id = “ @ + id / editdetail” android:layout_width = “ match_parent” android:layout_height = “ wrap_content” android:minLines =2/> <LinearLayout android:layout_width = “ fill_parent” android:layout_height = “ wrap_content” android:orientation = “ horizo​​ntal” > <Button android:id = “ @ + id / btnsave” android:layout_width = “ wrap_content” android:layout_height = “ wrap_content” android:text = “ @ string / btnwrite” /> <按鈕android:id = “ @ + id / btnclean” android:layout_width = “ wrap_content” android:layout_height = “ wrap_content” android:text = “ @ string / btnclean” /> </ LinearLayout> <Button android:id = “ @ + id / btnread” android:layout_width = “ wrap_content” android:layout_height = “ wrap_content” android:text = “ @ string / btnread” /> </ LinearLayout>

2.建立一個類,為檔案協助

FileHelper {
   

    私有上下文mContext ; 

    公共FileHelper (){ }  
    

    公共FileHelper (Context mContext ){ super (); 這個。mContext = mContext ; }  
        
        
    

    / *
    *這裡定義的是一個檔案儲存的方法,寫入到檔案中,所以是輸出流
    * * /公共空隙節省(字串檔名,字串filecontent )丟擲異常{ //這裡我們使用私有模式,創建出來的檔案只能被本應用訪問,還會覆蓋原檔案哦FileOutputStream中輸出= mContext 。openFileOutput (檔名,背景資訊。MODE_PRIVATE ); 
        輸出。寫(filecontent 。的getBytes ()); //將字串字串以位元組流的形式寫入到輸出流中
        輸出。close (); //關閉輸出流}
         
        
                    
    


    / *
    *這裡定義的是檔案讀取的方法
    * * / public String read (字串檔名)丟擲IOException { //開啟檔案輸入流FileInputStream input = mContext 。openFileInput (filename ); 位元組[] temp =新的位元組[ 1024 ];StringBuilder sb =新的StringBuilder (“” ); int len = 0 ; //讀取檔案內容:while ((len =
        
        
        
          
          
         
        
         輸入。讀取(溫度))> 0{ 
            sb 。append (新的String (temp ,0 ,len )); } //關閉輸入流
        input 。close (); 返回某人。toString (); }     
        
        
        
    

}

3.在主方法中寫出

public class mainactivity extends the view implemented by appcompatactivity. OnClickListener {



Private EditText editdetail; private button btnsave; private button btnclean; private button btnread; private context mcontext;








@Override protected void oncreate (bundle savedinstancestate) {super. onCreate (savedInstanceState );

The setcontentview of the. Layout. activity_ main );

mContext = getApplicationContext ();

bindViews (); }







Private bindviews (){

Editdetail = (EditText of) findviewbyid (ř). ID . editdetail );

Editname = (EditText of) findviewbyid (ř). ID . editname );

Btnclean = (button) findviewbyid (ř). ID . btnclean );

Btnsave = (button) findviewbyid (ř). ID . btnsave );

Btnread = (button) findviewbyid (ř). ID . btnread );



btnclean . setOnClickListener (this;

btnsave . setOnClickListener (this;

btnread .setOnClickListener (this; }





@Override public invalid onclick (view v) {switch (V). Getid ()) {case}. id 。 btnclean :

editdetail . setText (“” );

editname . Settext ("); rest; situation - [R. id . Btnsave: filehelper fhelper = new filehelper(







Mcontext); string filename = editname. getText (). Tostring(); string filedetail = editdetail. getText (). Tostring(); try{

fHelper .Save (file name, file detail); toast. Maketext (getapplicationcontext (), "data written successfully", toast. LENGTH_ SHORT ). Display ();}






(e exception){

e.Printstacktrace(); toast. Maketext (getapplicationcontext(), "data write failed", toast. LENGTH_ SHORT )。 Show ();} break; case - [R. id 。 Btnread: String details = '; filehelper fhelper2 = new filehelper (getapplicationcontext()); try








{string fname = editname. getText ()。 toString ();

detail = fHelper2 . Read (fname);} catch (IOException E){

e.Printstacktrace();} toast. Maketext (getapplicationcontext (), details, toast. LENGTH_ SHORT ). Show (); rest;}








} }

今天的執行結果執行不出來
在這裡插入圖片描述
說的sdk配置不行,我重新修改了更新,時間有點長就沒有管,看明天能否執行出來