Android——文件存储之外部存储(含源码下载)

每个 Android 设备都支持共享的外部存储用来保存文件,它也是手机中的存储介质。保存在外部存储的文件都是全局可读的,而且在用户使用 USB 连接电脑后,可以修改这些文件。在 Android程序中,对外部存储的文件进行操作时,需要使用 Environment 类的getExternalStorageDirectory()方法,该方法用来获取外部存储器的目录。

注意: 为了读、写外部存储上的数据,必须在应用程序的全局配置文件(AndroidManifest.xml)中添加读、写外部存储的权限。(Android版本大于6即大于API 19还需要动态获取权限)配置如下:

<!--开启在外部存储中创建与删除文件权限-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
 <!--开启向外部存储写入数据权限 -->
 <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

下面简单介绍一下外部存储创建文件的简单使用方法:

1.创建文件并写入文件

  //创建目录
  File dir = new File(Environment.getExternalStorageDirectory()+
           path + File.separator);
   if (!dir.exists()) {
       dir.mkdirs();
   }
   //创建文件
   File file = new File(dir, File.separator + filename);
   if (!file.exists()) {
       file.createNewFile();
   }
                
  FileOutputStream	fos = new FileOutputStream(file);
  fos.write(content);//写入文件
  fos.flush();//清除缓存
  fos.close();//关闭文件输出流

2.读取文件

File file = new File(Environment.getExternalStorageDirectory()+
        path + File.separator + filename);
//文件存在才执行以下操作
if (file.exists()){
    FileInputStream	fis = new FileInputStream(file);
    byte[]	buffer = new byte[fis.available()];
    fis.read(buffer);//读取文件并存入buffer中
    fis.close();//关闭文件输入流
 }

3.删除文件

File file = new File(Environment.getExternalStorageDirectory()+path,filename);
//存在就删除
if (file.exists()){
     file.delete();
 }

这里写入一个简单外部文件操作的类ExternalStorageHelper,包括读、写、删除和更改文件名。
ExternalStorageHelper.java

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExternalStorageHelper {
    //创建文件夹,并保存
    public static boolean saved(String path,String filename,byte[] content){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            FileOutputStream fos = null;

            try {
                //创建目录
                File dir = new File(Environment.getExternalStorageDirectory()+
                        path + File.separator);
                if (!dir.exists()) {
                    dir.mkdirs();
                }

                //创建文件
                File file = new File(dir, File.separator + filename);

                if (!file.exists()) {
                    file.createNewFile();
                }

                fos = new FileOutputStream(file);
                fos.write(content);
                fos.flush();
                return true;

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }

    //显示文件夹
    public static byte[] showFile(String path,String filename){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            FileInputStream fis = null;
            byte[] buffer;

            try {
                File file = new File(Environment.getExternalStorageDirectory()+
                        path + File.separator + filename);

                if (file.exists()){
                    fis = new FileInputStream(file);
                    buffer = new byte[fis.available()];
                    fis.read(buffer);
                    return buffer;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "".getBytes();
    }

    //删除指定文件
    public static boolean deleteFile(String path,String filename){
        File file = new File(Environment.getExternalStorageDirectory()+path,filename);

        if (file.exists()){
            file.delete();

            return true;
        }

        return false;
    }

    //更改文件名
    public static boolean renameFileName(String path,String currentFilename,String lastFilename){
        File currentFile = new File(Environment.getExternalStorageDirectory()+path,currentFilename);
        File lastFile = new File(Environment.getExternalStorageDirectory()+path,lastFilename);

        if (currentFile.exists() && (!lastFile.exists())){
            currentFile.renameTo(lastFile);
            return true;
        }

        return false;
    }
}

链接:百度网盘下载 提取码:053x

发布了33 篇原创文章 · 获赞 11 · 访问量 9257

猜你喜欢

转载自blog.csdn.net/qq_43567345/article/details/104456391