Java文件流输入输出
生活随笔
收集整理的這篇文章主要介紹了
Java文件流输入输出
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文件流讀入
import java.util.*; import java.io.*; public class Test {public static void main(String[] args) throws FileNotFoundException{Scanner in;in = new Scanner(new File("1.txt"));int temp;for (int i = 0; i < 5; ++i) {temp = in.nextInt();System.out.println(temp);}} }文件流輸出
import java.io.*;public class Test {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("2.txt");OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");PrintWriter out = new PrintWriter(osw, true);out.println("Hello world!");} }很有意思的一點(diǎn)是,如果只用了文件名,放到那個(gè)類的構(gòu)造的過(guò)程中,那得到的文件是空的!!
這個(gè)很有意思!
import java.io.*;public class Test {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("2.txt");PrintWriter out = new PrintWriter(fos, true);out.println("Hello world!!");} }上面的代碼也是可以的
注意看下這個(gè)代碼:
import java.io.*;public class Test {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("2.txt");PrintWriter out = new PrintWriter(fos, false);out.println("Hello world!!");} }我改成了false,這樣的結(jié)果,讓文件重新變?yōu)榭盏牧恕?/p>
總結(jié)
以上是生活随笔為你收集整理的Java文件流输入输出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java输入输出(标准)
- 下一篇: new Scanner(1.txt);读