在java程序中,對於數據的輸入/輸出操做以流(Stream)方式進行;J2SDK提供了各類各樣的「流」類,用以獲取不一樣種類的數據;程序中經過標準的方法輸入或輸出數據,I/O流提供一條通道程序,能夠使用這條通道把源中的字節序列送給目的地。把輸入流的指向稱做源,程序從指向源的輸入流中讀取源讀取源中的數據。而輸出流的指向是字節要去的一個目的地,程序經過向輸出流中寫入數據把信息傳遞到目的地。雖然I/O流常常與磁盤文件存取有關,可是程序的源和目的地也能夠是鍵盤、鼠標、內存或者顯示器窗口。java
輸入流經過使用read()方法從輸入流讀出源中的數據,輸出流經過用write()方法把數據寫入輸出流到達目的地。函數
讀/寫流的通常流程spa
讀(Reading)命令行
1.open a streamcode
2.while more informationorm
3. read information對象
4.close the stream
內存
寫(Writing)
get
open a stream
input
while more information
write information
close ths stream
兩種流的定義(讀取信息的基本數據單位)
字節流:一個字節一個字階的讀或者寫,可以輸出任何類型數據
字符流:一個字符一個字符讀或者寫,本質是基於字節流讀取字節時,去查指定的嗎表
輸入/輸出流的分類
java.io包中定義了多個流類型來實現輸入/輸出功能;能夠從不一樣的角度對其進行分類:
按照數據流的方向的不一樣能夠分爲輸出流輸出流
按照處理數據單位的不一樣能夠分爲字節流和字符流
按照功能不一樣能夠分爲字節流和處理流
標準輸入輸出
package com.zhong.test; import java.io.IOException; public class StandardStream { public static void main(String args[]) throws IOException{ byte b[] = new byte[10]; System.in.read(b); } }
scanner是隊System.in.read();的擴展,SYstem.in.read();是每按一次鍵接收到一個一個字符,而scanner則能夠根據不一樣的條件接受不一樣的值。
Scanner reader = new Scanner(System.in);
而後reader對象調用下列方法(函數),讀取用戶在命令行輸入的各類數據類型:
nextByte(),nextDouble(),nextFloat().nextInt(),nextLine(),nextLong(),nextShort()
上述方法都會形成阻塞,等待用戶在命令行輸入數據回車確認。
package com.zhong.test; import java.util.Scanner; public class ScannerDemo { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int s = sc.nextShort(); System.out.println("s = "+s); } }
文件的讀寫操做
文件讀/寫流程
1.打開文件流2.條件判斷3.讀出/寫入4.關閉文件流
兩種類型文件
FIleInputStream/FileOutputStream(字節流)
FileReader/FileWriter(字符流)
package com.zhong.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String args[]) throws IOException{ File inputfile = new File("/root/Desktop/targetfile.txt"); File outputfile = new File("/root/Desktop/resultfile.txt"); FileInputStream in = new FileInputStream(inputfile); FileOutputStream out = new FileOutputStream(outputfile); int c; while((c = in.read()) != -1){ out.write(c); } in.close(); out.close(); } }
提升文件讀寫效率
兩類緩衝流
針對字節流:
java.io.BufferedInputStream類
java.io.BufferedOutStream類
針對字符流
java.io.BufferedReader類
java.io.BufferedWriter類
IO的緩衝區的存在就是爲了提升效率,把要操做的數據存進緩衝區,而後一次性把緩衝區的內容寫道目的地,而不是寫一次就往目的地寫一次,因此創建緩衝區對象時,要先有流對象。
package com.zhong.test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class BufferRead { public static void main(String args[]) throws IOException{ String filename = "/root/Desktop/String.txt"; FileInputStream fis = new FileInputStream(filename); BufferedInputStream bis = new BufferedInputStream(fis); int count = 0; int c; while((c = bis.read())!=-1){ if(c == 'A'){ count++; } } fis.close(); bis.close(); System.out.println(""+count); } }
package com.zhong.test; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferWriter { public static void main(String args[]) throws IOException{ FileWriter fw = null; fw = new FileWriter("/root/Desktop/String.txt"); BufferedWriter bfw = new BufferedWriter(fw); bfw.write("asda2312asd\n"); bfw.write("aaaaaaasccc6656xzc23\n"); bfw.write("aa3as4f546xckjb asjkdbkjck\n"); bfw.flush(); bfw.close(); } }
package com.zhong.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Echo { public static void main(String args[]) throws IOException{ BufferedReader bw = new BufferedReader(new InputStreamReader(System.in)); String s; while((s = bw.readLine()).length()!=0){ System.out.println(s); } } }