java一次性读取文件

简单说明:

实际项目中,有很多情况需要将文件中的内容一次性读取进内存,进行一些业务的处理,这里给一个简单的案例



import java.io.*;

/**
 * @Author :feiyang
 * @Date :Created 2019/5/15
 */
public class ReadFileUtil {

    public String acceptFile(String filePath) throws IOException {
        File file = new File(filePath);
        byte[] bytes = new byte[(int) file.length()];
        FileInputStream fileInputStream = new FileInputStream(filePath);
        int ret  = fileInputStream.read(bytes);
        String content = new String(bytes, 0, ret);
        return content;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42133100/article/details/90243353