您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Android之文件读写工具类

Android之文件读写工具类

来源:二三娱乐

以下是主要内容,本类主要功能有:
1.创建文件功能;
2.向文件中写入字节数组;
3.向文件中写入字符串;
4.从文件中读取字节数组;
5.从文件中读取字符串;
6.遍历获取某目录下制定类型的所有文件


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件读写工具类
 * 
 * @author bear
 *
 */
public class FileUtil {
    /**
     * 遍历获取某目录下制定类型的所有文件
     * @param filePath
     * @param type
     * @return
     */
    public  List<String> getFileDir(String filePath,String type) {
        List<String> picList = new ArrayList<String>();
        try{
            File f = new File(filePath);
            File[] files = f.listFiles();// 列出所有文件
            // 将所有的文件存入ArrayList中,并过滤所有图片格式的文件
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (checkIsImageFile(file.getPath(),type)) {
                    picList.add(file.getPath());
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    // 返回得到的图片列表
    return picList;
    }
    // 检查扩展名,得到图片格式的文件
    private boolean checkIsImageFile(String fName,String type) {
        boolean isImageFile = false;
        // 获取扩展名
        String FileEnd = fName.substring(fName.lastIndexOf(".") + 1,
                fName.length()).toLowerCase();
        if (FileEnd.equals(type)) {
            isImageFile = true;
        } else {
            isImageFile = false;
        }

        return isImageFile;

    }
    /**
     * 如果文件不存在,就创建文件
     * 
     * @param path 文件路径
     * @return
     */
    public static String createIfNotExist(String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return path;
    }

    /**
     * 向文件中写入数据
     * 
     * @param filePath
     *            目标文件全路径
     * @param data
     *            要写入的数据
     * @return true表示写入成功  false表示写入失败
     */
    public static boolean writeBytes(String filePath, byte[] data) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(data);
            fos.close();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }

    /**
     * 从文件中读取数据
     * 
     * @param file
     * @return
     */
    public static byte[] readBytes(String file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            int len = fis.available();
            byte[] buffer = new byte[len];
            fis.read(buffer);
            fis.close();
            return buffer;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return null;

    }

    /**
     * 向文件中写入字符串String类型的内容
     * 
     * @param file
     *            文件路径
     * @param content
     *            文件内容
     * @param charset
     *            写入时候所使用的字符集
     */
    public static void writeString(String file, String content, String charset) {
        try {
            byte[] data = content.getBytes(charset);
            writeBytes(file, data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    /**
     * 从文件中读取数据,返回类型是字符串String类型
     * 
     * @param file
     *            文件路径
     * @param charset
     *            读取文件时使用的字符集,如utf-8、GBK等
     * @return
     */
    public static String readString(String file, String charset) {
        byte[] data = readBytes(file);
        String ret = null;

        try {
            ret = new String(data, charset);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ret;
    }


Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务