Android內部存儲示例含代碼

Android內部存儲示例

我們能夠保存或讀取設備的內部存儲空間中的數據。愛掏網 - it200.comFileInputStream和FileOutputStream類用于將數據讀寫到文件中。愛掏網 - it200.com

在這里,我們將要讀寫數據到設備的內部存儲空間。愛掏網 - it200.com

讀寫數據到Android內部存儲的示例

activity_main.xml

從工具欄中拖動2個EditText、2個TextView和2個Button,現在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 類

讓我們編寫代碼,從內部存儲器中讀取和寫入數據。愛掏網 - 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>

}
“““

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 亚洲最大激情中文字幕| 中文字幕中文字幕中中文| 视频二区中文字幕| 日日碰狠狠添天天爽无码| 国产主播福利在线观看| 两根一进一出啊灌满了视频| 疯狂吃奶freesex| 国产自产2023最新麻豆| 亚洲xxxx18| 蜜桃视频一区二区三区在线观看| 成人欧美一区二区三区1314| 伊人久久精品亚洲午夜| 91欧美激情一区二区三区成人 | 1024人成网站色| 日韩三级免费看| 午夜第九达达兔鲁鲁| 99热这里只有精品7| 欧洲女人牲交性开放视频| 国产专区第一页| jizz在线播放| 欧美丰满大乳大屁股流白浆| 国产剧情在线播放| www视频在线观看| 欧美在线观看视频网站| 国产国产成年年人免费看片| 丝袜美腿中文字幕| 欧美视频网站在线观看| 国产成人免费网站app下载 | 亚洲国产最大av| 蕾丝av无码专区在线观看| 天天做日日做天天添天天欢公交车 | 美国美女一级毛片免费全| 在线观看国产小视频| 久久精品欧美日韩精品| 良妇露脸附生活照15| 美女无遮挡拍拍拍免费视频| 久久精品麻豆日日躁夜夜躁| 亚洲国产精品成人AV在线| 国产在播放一区| 在线日本中文字幕| 日韩电影免费在线观看中文字幕|