大部分程序都需要進行輸入/輸出處理,比如從鍵盤讀取數據、從屏幕中輸出數據、從文件中寫數據等等。在 Java 中,把這些不同類型的輸入、輸出源抽象爲流(Stream),而其中輸入或輸出的數據則稱爲數據流(Data Stream),用統一的接口表示,從而使程序設計簡單明瞭
import java.io.IOException;
import java.io.InputStream;
import java.io.OutStream;
public class IO{
public void io (InputStream in , OutStream out) throws IOException{
byte[] buf = new byte[4096]; //4096是保證數組的容量,沒其他意思
//read 是一個字節一個字節地讀,字節流的結尾標誌是-1
int len = in.read(buf);
while(len != -1){
out.write(buf,0,len);
len = in.read(buf);
}
}
public static void main(String[] args) throws IOException{
IO io = new IO();
System.out.println("輸入字符:");
io.copy(System.in,System.out);
}
}