学习日志17

学习目标:

数据存储与访问

学习内容:

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配置不行,我重新修改了更新,时间有点长就没有管,看明天能否运行出来

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值