java read file at one time

Brief explanation:

In actual projects, there are many situations where it is necessary to read the contents of the file into the memory at one time and perform some business processing. Here is a simple case

 



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;
    }
}

 

Guess you like

Origin blog.csdn.net/qq_42133100/article/details/90243353
Recommended