【java】常用到的一些获取文件内容的方法

一.前奏准备,获取文件名,根据文件名获取路径

//文件路径名
    String path;

    public String getPath() {
        return path;
    }
    /**
     * 根据路径获取文件名
     * @return 文件名字符串
     */
    public String fileName() throws Exception {

        String paths = getPath();

        // 判空操作必须要有 , 处理方式不唯一 , 根据实际情况可选其一 。
        if(paths == null) {
            throw new Exception("路径不能为null"); // 处理方法一
        }

        int start=paths.lastIndexOf("/");
        int end=paths.lastIndexOf(".");
        if(start!=-1 && end!=-1){
            return paths.substring(start+1,end);//包含头不包含尾 , 故:头 + 1
        }else{
            return "";
        }
    }
//读取SD卡中文件的方法
    //定义读取文件的方法:
    public String readFromSD(String filename) throws IOException {
        StringBuilder sb = new StringBuilder("");
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //打开文件输入流
            FileInputStream input = new FileInputStream(filename);
            byte[] temp = new byte[1024];

            int len = 0;
            //读取文件内容:
            while ((len = input.read(temp)) > 0) {
                sb.append(new String(temp, 0, len));
            }
            //关闭输入流
            input.close();
        }
        return sb.toString();
    }

二,根据路径获取文件内容(最常用的方法)

/* @Title: readFileContent
     * @Description: 读取文件内容
     * @param filePath
     * @return
             */
    public static String readFileContent(String filePath) {
        StringBuilder result = new StringBuilder();
        try {
//          BufferedReader bfr = new BufferedReader(new FileReader(new File(filePath)));
            BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = bfr.readLine()) != null) {
                result.append(lineTxt).append("\n");
            }
            bfr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }
}

三.对于实体类要做一些定义,比如这里你用到的文件(后面的直接get和set自动生成)

public class FileEntity {
    Integer id;
    String title;
    String content;
    String time;
    String path;

    public FileEntity() {
    }

    public FileEntity(Integer id, String title, String content, String time, String path) {
        this.id = id;  //id
        this.title = title;  //标题
        this.content = content;  //内容
        this.time = time;   //最后更新时间
        this.path = path;   //文件内容
    }

    public Integer getId() {
        return id;
    }

    public FileEntity setId(Integer id) {
        this.id = id;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public FileEntity setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getContent() {
        return content;
    }

    public FileEntity setContent(String content) {
        this.content = content;
        return this;
    }

    public String getTime() {
        return time;
    }

    public FileEntity setTime(String time) {
        this.time = time;
        return this;
    }

    public String getPath() {
        return path;
    }

    public FileEntity setPath(String path) {
        this.path = path;
        return this;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_46601559/article/details/124913319