Java笔记-按行读取文件及一次性全部读取数据
生活随笔
收集整理的這篇文章主要介紹了
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;}?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Java笔记-按行读取文件及一次性全部读取数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino笔记-解决上传时出现avr
- 下一篇: Java笔记-使用logback按天生成