java IO流用法
生活随笔
收集整理的這篇文章主要介紹了
java IO流用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、BufferedWriter & BufferedReader 的用法
使用標(biāo)準(zhǔn)IO的例子
運(yùn)行結(jié)果
二、PrintWriter 用法
建議用 PrintWriter 代替 BufferedWriter,因?yàn)?PrintWriter 同時(shí)支持字符流和字節(jié)流操作,而 BufferedWriter 只支持字符流操作,而且PrintWriter的print、println方法可以接受任意類型的參數(shù),而BufferedWriter的write方法只能接受字符、字符數(shù)組和字符串。
以下是改用 PrintWriter 和文件流的用法示例
import java.io.*; public class TestIO {public static void main(String[] args) throws IOException {File file = new File("data.txt");FileOutputStream fileOutputStream = new FileOutputStream(file);int a = 1, b = 2;PrintWriter out = new PrintWriter(new OutputStreamWriter(fileOutputStream, "UTF-8"), true); // UTF-8編碼,自動(dòng)刷新緩沖區(qū)out.println(a + b);fileOutputStream.close();} }運(yùn)行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的java IO流用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: intellij 快捷键
- 下一篇: /dev/tcp 的简单应用