android实例教程

Today we will look into android internal storage. Android offers a few structured ways to store data. These include

今天,我们将研究android内部存储。 Android提供了一些结构化的方式来存储数据 。 这些包括

  • Shared Preferences共享首选项
  • Internal Storage内部存储器
  • External Storage外置储存
  • SQLite StorageSQLite存储
  • Storage via Network Connection(on cloud)通过网络连接存储(在云上)

In this tutorial we are going to look into the saving and reading data into files using Android Internal Storage.

在本教程中,我们将研究如何使用Android Internal Storage将数据保存和读取到文件中。

Android内部存储 (Android Internal Storage)

Android Internal storage is the storage of the private data on the device memory. By default, saving and loading files to the internal storage are private to the application and other applications will not have access to these files. When the user uninstalls the applications the internal stored files associated with the application are also removed. However, note that some users root their Android phones, gaining superuser access. These users will be able to read and write whatever files they wish.

Android内部存储是设备内存中私有数据的存储。 默认情况下,将文件保存和加载到内部存储是应用程序专用的,其他应用程序将无法访问这些文件。 当用户卸载应用程序时,与该应用程序关联的内部存储文件也将被删除。 但是,请注意,某些用户会扎根其Android手机,从而获得超级用户访问权限。 这些用户将能够读取和写入所需的任何文件。

在Android内部存储中读取和写入文本文件 (Reading and Writing Text File in Android Internal Storage)

Android offers openFileInput and openFileOutput from the Java I/O classes to modify reading and writing streams from and to local files.

Android从Java I / O类提供openFileInputopenFileOutput ,以修改本地文件之间的读写流。

  • openFileOutput(): This method is used to create and save a file. Its syntax is given below:

    FileOutputStream fOut = openFileOutput("file name",Context.MODE_PRIVATE);

    The method openFileOutput() returns an instance of FileOutputStream. After that we can call write method to write data on the file. Its syntax is given below:

    openFileOutput() :此方法用于创建和保存文件。 其语法如下:

    FileOutputStream fOut = openFileOutput("file name",Context.MODE_PRIVATE);

    方法openFileOutput()返回FileOutputStream一个实例。 之后,我们可以调用write方法在文件上写入数据。 其语法如下:

  • openFileInput(): This method is used to open a file and read it. It returns an instance of FileInputStream. Its syntax is given below:
    FileInputStream fin = openFileInput(file);

    After that, we call read method to read one character at a time from the file and then print it. Its syntax is given below:

    In the above code, string temp contains all the data of the file.

    openFileInput() :此方法用于打开文件并读取它。 它返回FileInputStream的实例。 其语法如下:

    FileInputStream fin = openFileInput(file);

    之后,我们调用read方法从文件中一次读取一个字符,然后打印它。 其语法如下:

    在上面的代码中,字符串temp包含文件的所有数据。

  • Note that these methods do not accept file paths (e.g. path/to/file.txt), they just take simple file names.请注意,这些方法不接受文件路径(例如path / to / file.txt),它们仅使用简单的文件名。

Android内部存储项目结构 (Android Internal Storage Project Structure)

Android内部存储示例代码 (Android Internal Storage Example Code)

The xml layout contains an EditText to write data to the file and a Write Button and Read Button. Note that the onClick methods are defined in the xml file only as shown below:

xml布局包含用于将数据写入文件的EditText以及“写入按钮”和“读取按钮”。 请注意,仅在xml文件中定义onClick方法,如下所示:

activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:padding="5dp"android:text="Android Read and Write Text from/to a File"android:textStyle="bold"android:textSize="28sp" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:layout_below="@+id/textView1"android:layout_marginTop="22dp"android:minLines="5"android:layout_margin="5dp"><requestFocus /></EditText><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Write Text into File"android:onClick="WriteBtn"android:layout_alignTop="@+id/button2"android:layout_alignRight="@+id/editText1"android:layout_alignEnd="@+id/editText1" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Read Text From file"android:onClick="ReadBtn"android:layout_centerVertical="true"android:layout_alignLeft="@+id/editText1"android:layout_alignStart="@+id/editText1" /></RelativeLayout>

The MainActivity contains the implementation of the reading and writing to files as it was explained above.

MainActivity包含对文件进行读写的实现,如上所述。

package com.journaldev.internalstorage;import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;public class MainActivity extends Activity {EditText textmsg;static final int READ_BLOCK_SIZE = 100;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textmsg=(EditText)findViewById(R.id.editText1);}// write text to filepublic void WriteBtn(View v) {// add-write text into filetry {FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE);OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);outputWriter.write(textmsg.getText().toString());outputWriter.close();//display file saved messageToast.makeText(getBaseContext(), "File saved successfully!",Toast.LENGTH_SHORT).show();} catch (Exception e) {e.printStackTrace();}}// Read text from filepublic void ReadBtn(View v) {//reading text from filetry {FileInputStream fileIn=openFileInput("mytextfile.txt");InputStreamReader InputRead= new InputStreamReader(fileIn);char[] inputBuffer= new char[READ_BLOCK_SIZE];String s="";int charRead;while ((charRead=InputRead.read(inputBuffer))>0) {// char to string conversionString readstring=String.copyValueOf(inputBuffer,0,charRead);s +=readstring;}InputRead.close();textmsg.setText(s);} catch (Exception e) {e.printStackTrace();}}
}

Here, a toast is displayed when data is successfully written into the internal storage and the data is displayed in the EditText itself on reading the data from the file.

在此,当成功将数据写入内部存储器时,将显示祝酒词,并且在从文件中读取数据时,数据将显示在EditText本身中。

The image shown below is the output of the project. The image depicts text being written to the internal storage and on clicking Read it displays back the text in the same EditText.

下图是项目的输出。 该图像描述了写入内部存储器的文本,单击“读取”后,它将在同一EditText中显示文本。

文件在哪里? (Where is the file located?)

To actually view the file open the Android Device Monitor from Tools->Android->Android Device Monitor.
The file is present in the folder data->data->{package name}->files as shown in the images below:

要实际查看文件,请从工具-> Android-> Android设备监视器中打开Android设备监视器。
该文件位于文件夹data-> data-> {package name}-> files中,如下图所示:

The file “mytextfile.txt” is found in the package name of the project i.e. com.journaldev.internalstorage as shown below:

在项目的程序包名称com.journaldev.internalstorage中可以找到文件“ mytextfile.txt” ,如下所示:

Download the final project for android internal storage example from below link.

从下面的链接下载android内部存储示例的最终项目。

Download Android Internal Storage Example Project下载Android内部存储示例项目

翻译自: https://www.journaldev.com/9383/android-internal-storage-example-tutorial

android实例教程

android实例教程_Android内部存储示例教程相关推荐

  1. android 导航抽屉_Android导航抽屉示例教程

    android 导航抽屉 In this tutorial we'll implement a Navigation Drawer in our android application. Androi ...

  2. android浮动按钮_Android浮动操作按钮示例教程

    android浮动按钮 Today we will learn about Android Floating Action Button. We'll discuss the FloatingActi ...

  3. android圆角视图_Android图库视图示例教程

    android圆角视图 Android Gallery is a View commonly used to display items in a horizontally scrolling lis ...

  4. Android本地数据持久化:内部存储和外部存储

    内部存储 /data/data/应用包名/shared_prefs /data/data/应用包名/databases /data/data/应用包名/files /data/data/应用包名/ca ...

  5. android internal storage 路径,内部存储InternalStorage和外部存储ExternalStorage-Android

    > 一个是清除缓存,另一个是清除数据;内部存储InternalStorage,外部存储ExternalStorage 彻底理解android中的内部存储与外部存储- http://blog.cs ...

  6. 免费下载谷歌maps软件_Android Google Maps示例教程

    免费下载谷歌maps软件 In this tutorial we'll discuss and implement some interesting features of android googl ...

  7. primefaces教程_Primefaces BlockUI组件示例教程

    primefaces教程 Primefaces BlockUI is used to block interactivity of JSF components with optional ajax ...

  8. primefaces教程_Primefaces仪表板组件示例教程

    primefaces教程 We've mentioned earlier, Primefaces is one of leading libraries that provide you set of ...

  9. primefaces教程_Primefaces FileUpload组件示例教程

    primefaces教程 Today we will look into the Primefaces FileUpload component. HTML provides you file inp ...

最新文章

  1. Ms Sql Server 基本管理脚本(1)
  2. 网上银行跨行转账收费最高相差25倍 省钱有窍门
  3. python连接mysql_Python连接MySQL
  4. Java 到底有没有析构函数呢?
  5. 微型计算机及接口技术试卷,微机原理及接口技术试题以及答案
  6. 服务器系统怎么用备份启动,如何用veeam给windows服务器做备份?
  7. java string常见操作题
  8. [高光谱] Hyperspectral-Classification Pytorch 的高光谱场景的通用类 HyperX
  9. 研究城市空间结构的入门级文献及书籍推荐(待更新)
  10. Python学习笔记
  11. 支持断点续传的大文件传输协议
  12. uni-app背景图片在手机上不显示问题
  13. 有道云笔记客户端的下载和安装、使用(博主推荐)
  14. js通过localStorage实现一周/一天免登陆
  15. kubernetes 入门实践
  16. Ubuntu18.04 实现串口通信
  17. 车辆仪表数显器E-mark认证流程是怎样的?
  18. 计算机英语软件系统介绍ppt,ppt软件电脑上显示英文
  19. 文物3D模型互动展示 | 足不出户,即可领略九龙壁的美轮美奂
  20. K8S云原生环境渗透学习

热门文章

  1. 扩展 delphi 泛型 以实现类似lambda功能 , C#中的any count first last 等扩展方法
  2. 朋友,决定了就去做.
  3. spring 连数据库的配置文件
  4. [转载] python 动态变量创建locals()
  5. [转载] Python中产生随机数
  6. 自定义 Android 钟表盘,这一篇就够了
  7. bzoj 2553 [BeiJing2011]禁忌——AC自动机+概率DP+矩阵
  8. 洛谷——P4053 [JSOI2007]建筑抢修
  9. VMWare关闭beep声
  10. [转]深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)...