Java笔记-按行读取文件及一次性全部读取数据

一行一行读取文件:

ClassPathResource classPathResource = new ClassPathResource("xxxx.txt");
InputStream inputStream =classPathResource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String txt = "";
while(reader.ready()) {

    txt += reader.readLine();
}

String s = new String(Base64.encodeBase64(txt.getBytes(UTF_8)), UTF_8);

一次性读取文件:

    private String getXXXXXXXFile(String fileName) throws IOException {

        ClassPathResource classPathResource = new ClassPathResource(fileName);
        File file = classPathResource.getFile();
        FileInputStream in = new FileInputStream(file);
        byte[] fileContent = new byte[(int) file.length()];
        in.read(fileContent);
        String txt = new String(fileContent);

        String pileString = new String(Base64.encodeBase64(txt.getBytes(UTF_8)), UTF_8);
        return pileString;
    }

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页