Android內(nèi)部存儲示例含代碼

    Android內(nèi)部存儲示例

    我們能夠保存或讀取設(shè)備的內(nèi)部存儲空間中的數(shù)據(jù)。愛掏網(wǎng) - it200.comFileInputStream和FileOutputStream類用于將數(shù)據(jù)讀寫到文件中。愛掏網(wǎng) - it200.com

    在這里,我們將要讀寫數(shù)據(jù)到設(shè)備的內(nèi)部存儲空間。愛掏網(wǎng) - it200.com

    讀寫數(shù)據(jù)到Android內(nèi)部存儲的示例

    activity_main.xml

    從工具欄中拖動2個EditText、2個TextView和2個Button,現(xiàn)在activity_main.xml文件將如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        tools:context=".MainActivity" >  
    
        <EditText  
            android:id="@+id/editText1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentRight="true"  
            android:layout_alignParentTop="true"  
            android:layout_marginRight="20dp"  
            android:layout_marginTop="24dp"  
            android:ems="10" >  
    
            <requestFocus />  
        </EditText>  
    
        <EditText  
            android:id="@+id/editText2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignRight="@+id/editText1"  
            android:layout_below="@+id/editText1"  
            android:layout_marginTop="24dp"  
            android:ems="10" />  
    
        <TextView  
            android:id="@+id/textView1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignBaseline="@+id/editText1"  
            android:layout_alignBottom="@+id/editText1"  
            android:layout_alignParentLeft="true"  
            android:text="File Name:" />  
    
        <TextView  
            android:id="@+id/textView2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignBaseline="@+id/editText2"  
            android:layout_alignBottom="@+id/editText2"  
            android:layout_alignParentLeft="true"  
            android:text="Data:" />  
    
        <Button  
            android:id="@+id/button1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignLeft="@+id/editText2"  
            android:layout_below="@+id/editText2"  
            android:layout_marginLeft="70dp"  
            android:layout_marginTop="16dp"  
            android:text="save" />  
    
        <Button  
            android:id="@+id/button2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignBaseline="@+id/button1"  
            android:layout_alignBottom="@+id/button1"  
            android:layout_toRightOf="@+id/button1"  
            android:text="read" />  
    
    </RelativeLayout>
    

    Activity 類

    讓我們編寫代碼,從內(nèi)部存儲器中讀取和寫入數(shù)據(jù)。愛掏網(wǎng) - it200.com

    “`java
    package example.javatpoint.com.internalstorage;

    import android.content.Context;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class MainActivity extends AppCompatActivity {
    EditText editTextFileName,editTextData;
    Button saveButton,readButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    <pre><code> editTextFileName=findViewById(R.id.editText1);
    editTextData=findViewById(R.id.editText2);
    saveButton=findViewById(R.id.button1);
    readButton=findViewById(R.id.button2);

    //Performing Action on Read Button
    saveButton.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View arg0) {
    String filename=editTextFileName.getText().toString();
    String data=editTextData.getText().toString();

    FileOutputStream fos;
    try {
    fos = openFileOutput(filename, Context.MODE_PRIVATE);
    //default mode is PRIVATE, can be APPEND etc.
    fos.write(data.getBytes());
    fos.close();

    Toast.makeText(getApplicationContext(),filename + " saved",
    Toast.LENGTH_LONG).show();

    } catch (FileNotFoundException e) {e.printStackTrace();}
    catch (IOException e) {e.printStackTrace();}

    }

    });

    //Performing Action on Read Button
    readButton.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View arg0) {
    String filename=editTextFileName.getText().toString();
    StringBuffer stringBuffer = new StringBuffer();
    try {
    //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(
    openFileInput(filename)));
    String inputString;
    //Reading data line by line and storing it into the stringbuffer
    while ((inputString = inputReader.readLine()) != null) {
    stringBuffer.append(inputString + "\n");
    }

    } catch (IOException e) {
    e.printStackTrace();
    }
    //Displaying data on the toast
    Toast.makeText(getApplicationContext(),stringBuffer.toString(),Toast.LENGTH_LONG).show();

    }

    });
    }
    </code></pre>

    }
    “““

    輸出:

    聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
    發(fā)表評論
    更多 網(wǎng)友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 国产精品一区二区四区| 日韩精品人妻一区二区中文八零| 偷拍精品视频一区二区三区| 中文字幕一区二区人妻| 无码一区二区三区亚洲人妻| 精品国产乱子伦一区二区三区| 成人免费视频一区二区| 成人精品一区久久久久| 51视频国产精品一区二区| 亚洲视频一区调教| 2014AV天堂无码一区| 日韩精品无码一区二区三区AV | 亚洲中文字幕久久久一区| 国产综合精品一区二区三区| 琪琪see色原网一区二区| 在线精品自拍亚洲第一区| 中文乱码字幕高清一区二区| 亚欧在线精品免费观看一区 | 91一区二区三区四区五区| 久久亚洲一区二区| 无码人妻精品一区二区三区久久| 亚洲熟女少妇一区二区| 日韩精品一区二区三区四区 | 亚洲中文字幕乱码一区| 中文字幕人妻第一区| 亚洲av无码成人影院一区| 亚洲精品精华液一区二区| 国产乱码精品一区二区三区四川人| 在线日产精品一区| 国产精品美女一区二区三区| 日本国产一区二区三区在线观看| 日本国产一区二区三区在线观看| 在线视频国产一区| 东京热无码av一区二区| 国产一区二区精品久久凹凸| 高清一区二区三区日本久| 精品少妇ay一区二区三区| 国产精品成人免费一区二区| 视频一区二区在线播放| 天堂Aⅴ无码一区二区三区| 精品人无码一区二区三区|