JAVA——一次性读取或者写入文本文件所有内容
生活随笔
收集整理的這篇文章主要介紹了
JAVA——一次性读取或者写入文本文件所有内容
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一次性讀取文本文件所有內容
public String readFileToString(String fileName) { String encoding = "UTF-8"; File file = new File(fileName); Long filelength = file.length(); byte[] filecontent = new byte[filelength.intValue()]; FileInputStream in=null;try { in = new FileInputStream(file); in.read(filecontent); return new String(filecontent, encoding);} catch (IOException e) { e.printStackTrace(); return null;} finally { try {in.close();} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();} } }一次性寫入文本文件所有內容?
public void saveStringTOFile(File file,String content){FileWriter writer=null;try {writer = new FileWriter(file);writer.write(content);} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();} finally {try {writer.close();} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}}參考文章
https://blog.csdn.net/testcs_dn/article/details/41982939
https://www.cnblogs.com/wylblogs/p/readFile.html
總結
以上是生活随笔為你收集整理的JAVA——一次性读取或者写入文本文件所有内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA Swing——设置JButto
- 下一篇: JAVA Swing——框架(JFram