(這部分比較抽象且寫的不是很好,可能還要再編輯)
【概述】
流:流是一系列數據,包括輸入流和輸出流。你能夠想象成黑客帝國的「代碼雨」,只要咱們輸入指令,這些數據就像水同樣流進流出了
IO:Input和OutPut,輸入和輸出文件
經過IO流,咱們能夠利用Java去讀取來自文件的數據(目前階段大可能是記事本里面的數據)
下面列舉了常見的流
java
由於咱們只是初步瞭解使用IO流,並不須要所有了解這些流,下面會逐步寫出現階段用到的流
在使用以前,別忘了打上你的import java.io;windows
【BufferedReader】
BufferedReader類從字符輸入流中讀取文本並緩衝字符,以便有效地讀取字符,數組和行
由Reader構成的每一個讀取請求都會致使相應的讀取請求由基礎字符或字節流構成,建議經過BufferedReader包裝Reader的實例類以提升效率
能夠暫時把BufferedReader理解爲一個存儲數據的,「緩衝流」數組
import java.io.*; public class BRReadLines{ public static void main(String args[]) throws IOException{ BufferedReaderbr= new BufferedReader(new InputStreamReader(System.in)); String str;System.out.println("Enter lines of text."); System.out.println("Enter 'end' to quit."); do { str = br.readLine(); System.out.println(str); } while (!str.equals("end")); } }
【FileInputStream】
選擇一個現有文件做爲輸入流,這個路徑能夠在文件的「屬性」裏面複製,另外當文件處在原程序的文件夾裏面,能夠只寫文件名不用寫所有路徑
InputStreamf = new FileInputStream("C:/java/hello");
app
或者eclipse
File f = new File("C:/java/hello"); InputStreamout = new FileInputStream(f);
FileInputStream中的一些方法
public void close() throws IOException{}
protected void finalize()throws IOException{}
public int read(int r)throws IOException{}
public int read(byte[] r) throws IOException{}
public int available() throws IOException{}ui
【FileOutputStream】
有Input就有Output
OutputStream f= new FileOutputStream("C:/java/hello");
或者this
File f= new File("C:/java/hello"); OutputStream f= new FileOutputStream(f);
FileOutputStream中的一些方法
public void close() throws IOException{}
protected void finalize()throws IOException{}
public void write(int w)throws IOException{}spa
【一些實例代碼】
A3d
public static void main(String args[]) throws IOException { File f = new File("C:/Users/zhang/eclipse-workspace/HelloWord/src/lecture13/a1.txt"); // Make sure the path is correct! // path coped from windows is C:\Users\zhang\eclipse-workspace\HelloWord\src\lecture13 FileOutputStream fop = new FileOutputStream(f); // Create FileOutputStream object, a new file will be created if it does not exist. OutputStreamWriter writer = new OutputStreamWriter(fop, "gbk"); // Create OutputStreamWriter object, second argument is data format, gbk for windows, UTF-8 for Linux writer.append("Hello"); // Appends the specified character sequence to this writer. writer.append("\n"); // Appends a line return to this writer. writer.append("CS161FZ"); // Appends the specified character sequence to this writer. writer.close(); //Closes the stream, flushing it first. fop.close(); // Closes this file output stream and releases any system resources associated with this stream. FileInputStream fip = new FileInputStream(f); // Create a FileInputStream對 object InputStreamReader reader = new InputStreamReader(fip, "gbk"); // Create a InputStreamReader object, same data format with the above StringBuffer sb = new StringBuffer(); while (reader.ready()) { sb.append((char) reader.read()); // convert to char, and add to StringBuffer object } System.out.println(sb.toString()); reader.close(); // close read stream fip.close(); // Closes this file input stream and releases any system resources associated with the stream. }
Bcode
public static void main(String[] args) throws IOException { File f = new File("C:/Users/zhang/eclipse-workspace/HelloWord/src/lecture13/test.txt"); FileOutputStream fop = new FileOutputStream(f); OutputStreamWriter writer = new OutputStreamWriter(fop, "gbk"); int datatoWrite[] = {11, 21, 3, 40, 5, 74, 89}; for (int i = 0; i < datatoWrite.length; i++) { writer.append(Integer.toString(datatoWrite[i])); // writes the ints writer.append("\n"); } writer.close(); // If you forget to close the writer, YOU CAN NOT SUCESSFULLY WRITER! fop.close(); FileInputStream fip = new FileInputStream(f); BufferedReader br = new BufferedReader(new InputStreamReader(fip, "gbk")); while(br.ready()) { System.out.println(br.readLine()); } br.close(); fip.close(); }