聲明:本系列只供本人自學(xué)使用图云,勿噴。
實(shí)際開發(fā)中最常用的就是文件IO邻邮,通常情況下竣况,操作二進(jìn)制文件用字節(jié)流,文本文件用字符流筒严。
一丹泉、文件字節(jié)流
1.FileInputStream
- 構(gòu)造器
//實(shí)際調(diào)用FileInputStream(File file)
public FileInputStream(String name) throws FileNotFoundException
public FileInputStream(File file) throws FileNotFoundException{
1.文件安全及權(quán)限檢查
2.創(chuàng)建FileDescriptor實(shí)例來表示文件連接
3.native方法open
}
// 使用文件系統(tǒng)的現(xiàn)有文件連接來創(chuàng)建
public FileInputStream(FileDescriptor fdObj)
- 核心方法
核心:私有的本地readBytes方法
private native int readBytes(byte b[], int off, int len) throws IOException;
public int read(byte b[]) throws IOException
public int read(byte b[], int off, int len)
public void close() throws IOException
- 其他方法
public native long skip(long n) throws IOException;
public native int available() throws IOException;
public final FileDescriptor getFD() throws IOException
public FileChannel getChannel()
2.FileOutputStream
- 構(gòu)造器
//實(shí)際調(diào)用FileOutputStream(File file, boolean append)
public FileOutputStream(String name) throws FileNotFoundException
public FileOutputStream(String name, boolean append)
public FileOutputStream(File file) throws FileNotFoundException
public FileOutputStream(File file, boolean append) throws FileNotFoundException{
1.文件安全及權(quán)限檢查
2.創(chuàng)建FileDescriptor實(shí)例來表示文件連接
3.native方法open
}
// 使用文件系統(tǒng)的現(xiàn)有文件連接來創(chuàng)建
public FileOutputStream(FileDescriptor fdObj)
- 核心方法
核心:私有的本地writeBytes方法
private native void writeBytes(byte b[], int off, int len, boolean append)
public void write(byte b[]) throws IOException
public void write(byte b[], int off, int len)
public void close() throws IOException
- 其他方法
public final FileDescriptor getFD() throws IOException
public FileChannel getChannel()
3.demo
最常見的功能:用FileInputStream和FileOutputStream復(fù)制文件
注:JDK7新增try-with-resources語法,將資源放入try()內(nèi)萝风,就不用手動close()了嘀掸。
try (
InputStream is = new FileInputStream("D:\\fileTest\\app.properties");
// InputStream is=new FileInputStream(new File("D:\\fileTest\\app.properties"));
OutputStream os = new FileOutputStream("D:\\fileTest\\out.properties");
) {
byte[] bytes=new byte[1024];
int len=0;
while ((len=is.read(bytes))!=-1){
os.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
二紫岩、文件字符流
在研究FileReader和FileWriter之前规惰,有必要先了解其父類。
1.InputStreamReader
底層StreamDecoder
- 構(gòu)造器(必須以InputStream為入?yún)ⅲ?/strong>
// 實(shí)際調(diào)用 StreamDecoder的構(gòu)造器
public InputStreamReader(InputStream in)
public InputStreamReader(InputStream in, String charsetName)
public InputStreamReader(InputStream in, Charset cs)
public InputStreamReader(InputStream in, CharsetDecoder dec)
- 核心方法
核心:read到字符數(shù)組
public int read(char cbuf[], int offset, int length) throws IOException
public int read() throws IOException
public void close() throws IOException
- 其他方法
public boolean ready() throws IOException
public String getEncoding()
2.FileReader
本質(zhì):給定默認(rèn)字符集與buffer大小的InputStreamReader
- 構(gòu)造器
public FileReader(String fileName) throws FileNotFoundException
public FileReader(File file) throws FileNotFoundException
public FileReader(FileDescriptor fd)
3.OutputStreamWriter
底層StreamEncoder
- 構(gòu)造器(必須以O(shè)utputStream為入?yún)ⅲ?/strong>
// 實(shí)際調(diào)用 StreamEncoder的構(gòu)造器
public OutputStreamWriter(OutputStream out)
public OutputStreamWriter(OutputStream out, String charsetName)
public OutputStreamWriter(OutputStream out, Charset cs)
public OutputStreamWriter(OutputStream out, CharsetEncoder enc)
- 核心方法
核心:將字符數(shù)組write到輸出流
public void write(String str, int off, int len) throws IOException
public void write(char cbuf[], int off, int len) throws IOException
public void write(int c) throws IOException
public void flush() throws IOException{
1.flushBuffer()
2.flush輸出流
}
public void close() throws IOException
- 其他方法
void flushBuffer() throws IOException
public String getEncoding()
4.FileWriter
本質(zhì):給定默認(rèn)字符集與buffer大小的OutputStreamWriter
- 構(gòu)造器
public FileWriter(String fileName) throws IOException
public FileWriter(String fileName, boolean append) throws IOException
public FileWriter(File file) throws IOException
public FileWriter(File file, boolean append) throws IOException
public FileWriter(FileDescriptor fd)
5.demo
功能1:用FileReader和FileWriter復(fù)制文本文件
注:和上述的字節(jié)流傳輸代碼相似泉蝌,只是換成用char[]傳輸歇万,因此需要的話可以自行輸出方便閱讀的chars
try(
FileReader fileReader=new FileReader("D:\\fileTest\\app.properties");
FileWriter fileWriter=new FileWriter("D:\\fileTest\\out2.properties")
) {
char[] chars=new char[1024];
int len=0;
while ((len=fileReader.read(chars))!=-1){
fileWriter.write(chars,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
功能2:用InputStreamReader讀取properties
InputStreamReader reader=new InputStreamReader(new FileInputStream("D:\\fileTest\\app.properties"));
Properties properties=new Properties();
properties.load(reader);
properties.forEach((k,v)-> System.out.println(k+":"+v));
三、總結(jié)
本節(jié)研究了基礎(chǔ)的文件操作類勋陪,包括字節(jié)流FileInputStream贪磺、FileOutputStream,字符流InputStreamReader及其子類FileReader诅愚,OutputStreamWriter及其子類FileWriter寒锚,注意文件字符流必須通過字節(jié)流來構(gòu)造,這些類都可以實(shí)現(xiàn)讀寫文件,一般情況下我們使用字節(jié)流讀寫二進(jìn)制文件刹前,字符流讀寫文本文件(比如properties等包含中文的配置文件)泳赋。
下一節(jié)我們將探討使用較頻繁的Object流。