直接插在文件上,讀寫文件數(shù)據(jù)
創(chuàng)建對(duì)象
new FileOutputStream(文件)
不管文件是否存在,都會(huì)信件一個(gè)空文件
new FileOutputStream(文件,true)
文件存在,追加數(shù)據(jù)
高級(jí)流、操作流
與其他流對(duì)接,提供特點(diǎn)的數(shù)據(jù)處理功能
對(duì)高級(jí)流的操作,會(huì)對(duì)詳解的流執(zhí)行相同操作
FileOutputStream
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1OutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("f3");
out.write(97);//00 00 00 61->61
out.write(98);//00 00 00 62->62
out.write(99);//00 00 00 63->63
out.write(356);//00 00 01 64->64
byte[] a={
101,102,103,104,105,
106,107,108,109,110
};
out.write(a);
out.write(a, 2, 4);
out.flush();
out.close();
System.out.println("寫入完成");
}
}
運(yùn)行結(jié)果
寫入完成
FileInputStream
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class Test2InputStream {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("f3");
//單字節(jié)讀取 5個(gè)一批
readByte(in);
in.close();
//流只能順序讀取一次
in = new FileInputStream("f3");
//批量讀取
readMore(in);
in.close();
}
private static void readMore(FileInputStream in) throws IOException {
byte[] buff=new byte[5];
int n;
while((n=in.read(buff))!=-1)
{
System.out.println(n+" "+Arrays.toString(buff));
}
System.out.println();
}
private static void readByte(FileInputStream in) throws IOException {
int b;
while((b=in.read())!=-1)
{
System.out.print(b+" ");
}
System.out.println();
}
}
運(yùn)行結(jié)果
97 98 99 100 101 102 103 104 105 106 107 108 109 110 103 104 105 106
5 [97, 98, 99, 100, 101]
5 [102, 103, 104, 105, 106]
5 [107, 108, 109, 110, 103]
3 [104, 105, 106, 110, 103]