FileInputStream、FileReader、FileInputStream、FileWriter使用小結

本文是基於Linux環境運行,讀者閱讀前須要具有必定Linux知識java

InputStream包含以下三個方法:算法

  • int read():從輸入流中讀取單個字節,返回所讀取的字節數據(字節數據可直接轉化爲int類型)
  • int read(byte[] b):從輸入流中最多讀取b.length個字節的數據,並將其存儲在字節數組b中,返回實際讀取的字節數
  • int read(byte[] b, int off, int len):從輸入流中最多讀取len個字節的數據,並將其存儲在數組b中;放入數組b中時,並非從數組起點開始,而是從off位置開始,返回實際讀取的字節數

FileInputStream繼承InputStream,使用FileInputStream讀取文件內容編程

代碼1-1數組

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamTest {

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			throw new RuntimeException("請輸入路徑");
		}
		FileInputStream fis = null;
		try {
			byte[] bbuf = new byte[1024];
			int hasRead = 0;
			fis = new FileInputStream(args[0]);
			while ((hasRead = fis.read(bbuf)) > 0) {
				System.out.print(new String(bbuf, 0, hasRead));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

 

代碼1-1運行結果:bash

root@lejian:/home/software/.io# cat text 
Hello Spring
Hello Hibernate
Hello MyBatis
root@lejian:/home/software/.io# java FileInputStreamTest text 
Hello Spring
Hello Hibernate
Hello MyBatis

 

Reader包含以下三個方法:數據結構

  • int read():從輸入流中讀取單個字符,返回從讀取的字符數據(字符數據可直接轉化爲int類型)
  • int read(char cbuf[]):從輸入流中最多讀取cbuf.length個字符的數據,並將其存儲在字符數組的cbuf中,返回實際讀取的字符數
  • int read(char cbuf[], int off, int len):從輸入流中最多讀取len個字符的數據,並將其存儲在數組b中;放入數組cbuf中時,並非從數組起點開始,而是從off位置開始,返回實際讀取的字節數

FileReader的父類繼承自Reader,使用FileReader讀取文件spa

代碼1-2指針

import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			throw new RuntimeException("請輸入路徑");
		}
		FileReader fr = null;
		try {
			fr = new FileReader(args[0]);
			char[] cbuf = new char[32];
			int hasRead = 0;
			while ((hasRead = fr.read(cbuf)) > 0) {
				System.out.print(new String(cbuf, 0, hasRead));
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}

 

代碼1-2運行結果:blog

root@lejian:/home/software/.io# cat text 
Java編程思想
算法
編譯原理
root@lejian:/home/software/.io# java FileReaderTest text 
Java編程思想
算法
編譯原理

 

代碼1-3繼承

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamTest {

	public static void main(String[] args) {
		if (args == null || args.length < 2) {
			throw new RuntimeException("請輸入兩個路徑");
		}
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(args[0]);
			fos = new FileOutputStream(args[1]);
			byte[] bbuf = new byte[32];
			int hasRead = 0;
			while ((hasRead = fis.read(bbuf)) > 0) {
				fos.write(bbuf, 0, hasRead);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

 

OutputStream和Writer:

  • void write(int b):將指定的字節/字符輸出到輸出流中,其中c表明字節,也能夠表明字符
  • void write(byte b[]/char cbuf[]):將字節數組/字符數組中的數據輸出到指定輸出流中
  • void write(byte b[]/char cbuf[], int off, int len):將字節數組/字符數組中從off位置開始,長度爲len的字節/字符輸出到輸出流中

Writer裏還包含以下兩個方法:

  • void write(String str):將str字符串裏包含的字符輸出到指定輸出流中
  • void write(String str, int off, int len):將str字符串裏從off位置開始,長度爲len的字符輸出到指定的輸出流中

 

使用FileInputStream來執行輸入,FileOutputStream來執行輸出

代碼1-3運行結果:

root@lejian:/home/software/.io# cat input 
Java編程思想
算法
編譯原理
root@lejian:/home/software/.io# java FileOutputStreamTest input output 
root@lejian:/home/software/.io# cat output 
Java編程思想
算法
編譯原理

 

使用FileWriter來執行輸出

代碼1-4

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			throw new RuntimeException("請輸入路徑");
		}
		FileWriter fw = null;
		try {
			fw = new FileWriter(args[0]);
			fw.write("數據結構與算法\n");
			fw.write("Python基礎教程\n");
			fw.write("C和指針\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fw != null) {
					fw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

 

代碼1-4運行結果:

root@lejian:/home/software/.io# java FileWriterTest book 
root@lejian:/home/software/.io# cat book 
數據結構與算法
Python基礎教程
C和指針

 

Java7以後,不少IO資源類都實現了AutoCloseable接口,只要在try語句中聲明的IO流均可以自動關閉,但有一些IO流像FileWriter這樣的輸出流,若是IO流不顯示的關閉,輸出流緩衝區的數據沒法flush到實際的物理節點,由於執行IO流的close()方法前,會先執行flush()方法

相關文章
相關標籤/搜索