• Android 关于文件及文件夹的创建 、删除、重命名、复制拷贝


    package com.example.administrator.myapplication.util;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.RandomAccessFile;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;

    /**
    * Created by Administrator on 2019/5/17 0017.
    */

    public class FileUtils {

    private static final String TAG = "FileUtils";

    /**
    * 创建文件
    *
    * @param filePath 文件地址
    * @param fileName 文件名
    * @return
    */
    public static boolean createFile(String filePath, String fileName) {

    String strFilePath = filePath + fileName;

    File file = new File(filePath);
    if (!file.exists()) {
    /** 注意这里是 mkdirs()方法 可以创建多个文件夹 */
    file.mkdirs();
    }

    File subfile = new File(strFilePath);

    if (!subfile.exists()) {
    try {
    boolean b = subfile.createNewFile();
    return b;
    } catch (IOException e) {
    e.printStackTrace();
    }
    } else {
    return true;
    }
    return false;
    }

    /**
    * 遍历文件夹下的文件
    *
    * @param file 地址
    */
    public static List<File> getFile(File file) {
    List<File> list = new ArrayList<>();
    File[] fileArray = file.listFiles();
    if (fileArray == null) {
    return null;
    } else {
    for (File f : fileArray) {
    if (f.isFile()) {
    list.add(0, f);
    } else {
    getFile(f);
    }
    }
    }
    return list;
    }

    /**
    * 删除文件
    *
    * @param filePath 文件地址
    * @return
    */
    public static boolean deleteFiles(String filePath) {
    List<File> files = getFile(new File(filePath));
    if (files.size() != 0) {
    for (int i = 0; i < files.size(); i++) {
    File file = files.get(i);

    /** 如果是文件则删除 如果都删除可不必判断 */
    if (file.isFile()) {
    file.delete();
    }

    }
    }
    return true;
    }


    /**
    * 向文件中添加内容
    *
    * @param strcontent 内容
    * @param filePath 地址
    * @param fileName 文件名
    */
    public static void writeToFile(String strcontent, String filePath, String fileName) {
    //生成文件夹之后,再生成文件,不然会出错
    String strFilePath = filePath + fileName;
    // 每次写入时,都换行写

    File subfile = new File(strFilePath);


    RandomAccessFile raf = null;
    try {
    /** 构造函数 第二个是读写方式 */
    raf = new RandomAccessFile(subfile, "rw");
    /** 将记录指针移动到该文件的最后 */
    raf.seek(subfile.length());
    /** 向文件末尾追加内容 */
    raf.write(strcontent.getBytes());

    raf.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }


    /**
    * 修改文件内容(覆盖或者添加)
    *
    * @param path 文件地址
    * @param content 覆盖内容
    * @param append 指定了写入的方式,是覆盖写还是追加写(true=追加)(false=覆盖)
    */
    public static void modifyFile(String path, String content, boolean append) {
    try {
    FileWriter fileWriter = new FileWriter(path, append);
    BufferedWriter writer = new BufferedWriter(fileWriter);
    writer.append(content);
    writer.flush();
    writer.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * 读取文件内容
    *
    * @param filePath 地址
    * @param filename 名称
    * @return 返回内容
    */
    public static String getString(String filePath, String filename) {
    FileInputStream inputStream = null;
    try {
    inputStream = new FileInputStream(new File(filePath + filename));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    InputStreamReader inputStreamReader = null;
    try {
    inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    }
    BufferedReader reader = new BufferedReader(inputStreamReader);
    StringBuffer sb = new StringBuffer("");
    String line;
    try {
    while ((line = reader.readLine()) != null) {
    sb.append(line);
    sb.append(" ");
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    return sb.toString();
    }

    /**
    * 重命名文件
    *
    * @param oldPath 原来的文件地址
    * @param newPath 新的文件地址
    */
    public static void renameFile(String oldPath, String newPath) {
    File oleFile = new File(oldPath);
    File newFile = new File(newPath);
    //执行重命名
    oleFile.renameTo(newFile);
    }


    /**
    * 复制文件
    *
    * @param fromFile 要复制的文件目录
    * @param toFile 要粘贴的文件目录
    * @return 是否复制成功
    */
    public static boolean copy(String fromFile, String toFile) {
    //要复制的文件目录
    File[] currentFiles;
    File root = new File(fromFile);
    //如同判断SD卡是否存在或者文件是否存在
    //如果不存在则 return出去
    if (!root.exists()) {
    return false;
    }
    //如果存在则获取当前目录下的全部文件 填充数组
    currentFiles = root.listFiles();

    //目标目录
    File targetDir = new File(toFile);
    //创建目录
    if (!targetDir.exists()) {
    targetDir.mkdirs();
    }
    //遍历要复制该目录下的全部文件
    for (int i = 0; i < currentFiles.length; i++) {
    if (currentFiles[i].isDirectory())//如果当前项为子目录 进行递归
    {
    copy(currentFiles[i].getPath() + "/", toFile + currentFiles[i].getName() + "/");

    } else//如果当前项为文件则进行文件拷贝
    {
    CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName());
    }
    }
    return true;
    }


    //文件拷贝
    //要复制的目录下的所有非子目录(文件夹)文件拷贝
    public static boolean CopySdcardFile(String fromFile, String toFile) {

    try {
    InputStream fosfrom = new FileInputStream(fromFile);
    OutputStream fosto = new FileOutputStream(toFile);
    byte bt[] = new byte[1024];
    int c;
    while ((c = fosfrom.read(bt)) > 0) {
    fosto.write(bt, 0, c);
    }
    fosfrom.close();
    fosto.close();
    return true;

    } catch (Exception ex) {
    return false;
    }
    }

    }


    --------------------- 

  • 相关阅读:
    CentOS
    Ubuntu
    Ubuntu
    Ubuntu
    2020-10-12 分享——Bigdata & ML Development WITH SCALA/python & SPARK (待续)
    postgresql 自增列 初始值设置
    白话布隆过滤器BloomFilter(转发)
    Typora ---一款简洁的Markdown编辑器
    postman —— API测试工具 && HTTP请求 (filddler 同类)
    K8S —— 学习资料(待看)
  • 原文地址:https://www.cnblogs.com/hyhy904/p/10995896.html
Copyright © 2020-2023  润新知