IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> Android:文件存储 -> 正文阅读

[开发测试]Android:文件存储

Android-File

Android使用Linux的文件系统,开发人员可以建立和访问程序自身建立的私有文件,也可以访问保存在资源目录中的原始文件和XML文件,还可以将文件保存在TF卡等外部存储设备中

内部存储

Android系统允许应用程序创建仅能够自身访问的私有文件,文件保存在设备的内部存储器上,在Android系统下的/data/data//files目录中

Android系统不仅支持标准Java的IO类和方法,还提供了能够简化读写流式文件过程的函数

openFileOutput()函数

? openFileOutput()函数为写入数据做准备而打开文件

? 如果指定的文件存在,直接打开文件准备写入数据

? 如果指定的文件不存在,则创建一个新的文件

? openFileOutput()函数的语法格式如下:

? public FileOutputStream openFileOutput(String name, int mode)

? ?第1个参数是文件名称,这个参数不可以包含描述路径的斜杠

? ?第2个参数是操作模式,Android系统支持四种文件操作模式

? ?函数的返回值是FileOutputStream类型

四种文件操作模式

模式说明
MODE_PRIVATE私有模式,缺陷模式,文件仅能够被创建文件的程序访问,或具有相同UID的程序访问。
MODE_APPEND追加模式,如果文件已经存在,则在文件的结尾处添加新数据。
MODE_WORLD_READABLE全局读模式,允许任何程序读取私有文件。
MODE_WORLD_WRITEABLE全局写模式,允许任何程序写入私有文件。

openFileInput()函数

? openFileInput()函数为读取数据做准备而打开文件

? openFileInput()函数的语法格式如下:

? public FileInputStream openFileInput (String name)

? 第1个参数也是文件名称,同样不允许包含描述路径的斜杠

? 使用openFileInput()函数打开已有文件,并以二进制方式读取数据的

读取文件的一般步骤

1.使用openFileInput()获得FileInputStream对象

2.通过FileInputStream对象的read()方法读取数据

3.关闭FileInputStream对象

向文件写入数据的一般步骤

1.使用openFileOutput()获得FileOutputStream对象,如果mode为MODE_PRIVATE,则文件不存在时创建文件,文件存在时删除文件内容;如果mode为MODE_APPEND,则文件不存在时创建文件,文件存在时在最后追加。

2.通过FileOutputStream对象的write()方法写入数据

3.关闭FileOutputStream对象

openFileOutput()函数

为了提高文件系统的性能,一般调用write()函数时,如果写入的数据量较小,系统会把数据保存在数据缓冲区中,等数据量积攒到一定程度时再将数据一次性写入文件

因此,在调用close()函数关闭文件前,务必要调用flush()函数,将缓冲区内所有的数据写入文件

如果开发人员在调用close()函数前没有调用flush(),则可能导致部分数据丢失

示例代码:

MainActivity.java

package com.example.filedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

    private Button readable;
    private Button writeable;
    private EditText write;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String fileName="file_demo.txt";

        readable=(Button) findViewById(R.id.readable);
        writeable=(Button) findViewById(R.id.writeable);
        write=(EditText) findViewById(R.id.write);

        writeable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    FileOutputStream output=openFileOutput(fileName,MODE_PRIVATE);
                    String content=write.getText().toString();
                    BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(output);
                    bufferedOutputStream.write(content.getBytes());
                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();

                    output.close();
                    bufferedOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        readable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    FileInputStream input=openFileInput(fileName);
                    BufferedInputStream inputStream=new BufferedInputStream(input);
                    byte[] content = new byte[1024];
                    int length=inputStream.read(content);
                    Log.d("length", String.valueOf(length));

                    String string=new String(content,"utf-8");

                    Log.d("byte",string);

                    Toast.makeText(MainActivity.this, "info:"+string, Toast.LENGTH_SHORT).show();

                    inputStream.close();
                    input.close();

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/writeable"
        android:text="Write data to file"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/readable"
        android:text="Read data from file"
        app:layout_constraintTop_toBottomOf="@id/writeable"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/write"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

源码:https://github.com/cc-yy831128/Android-File

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章           查看所有文章
加:2021-09-13 09:33:56  更:2021-09-13 09:34:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 2:11:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码