1、流的概念
Java 語言采用流的機制來實作輸入/輸出,
所謂流,就是資料的有序排列,
而流可以是從某個源(稱為流源,Source of Stream)出來,
到某個目的地(稱為流匯 ,Sink of Stream)去的,
1.1、流的分類
- 流向分:輸入流、輸出流
- 流源分:字符流、位元組流
- 功能分:節點流、處理流
java 中統一將流的類放在 java.io 包下,
1.2、流的處理程序
用來處理流的類又叫做流處理器,
例如:Java 中可以使用 FileInputStream 從一個檔案中讀取資料,FileInputStream 就是一個流處理器

類似的也可以通過FileOutputStream向一個檔案中寫入資料,

Java 中提供了一種稱作為鏈接的機制,可以將一個流處理器與另一個流處理器首尾相連,形成一個流管道的鏈接,

類似的也可以向一個檔案中寫入資料

2、常用流的使用
FileInputStream和FileOutputStream:用于讀取和寫入諸如影像資料之類的原始位元組流
public static void main(String[] args){
try{
File oldFile=new File("D:\\SoftMgr\\img.jpg");//從本地獲取檔案物件
File newFile=new File("D:\\SoftMgr\\new.jpg");//寫入本地路徑新檔案
FileInputStream in=new FileInputStream(oldFile);//用輸入流讀取檔案的內容
FileOutputStream out=new FileOutputStream(newFile);//用輸出流把內容寫入新檔案
int len=0;
//如果沒有讀到檔案某尾(-1代表讀到檔案某尾),輸出流持續寫
while((len=in.read())!=-1){
out.write(len);
}
//流用完后必須要關閉,先打開的后關閉,后打開的先關閉
out.close();
in.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
ObjectInputStream和ObjectOutputStream:
物件流,ObjectOutputStream 和ObjectInputStream 分別與FileOutputStream 和FileInputStream 一起使用時,可以為應用程式提供對物件圖形的持久存盤,
ObjectInputStream 用于恢復那些以前序列化的物件,ObjectInputStream 對以前使用ObjectOutputStream 寫入的基本資料和物件進行反序列化,
//將物件保存到檔案必須實作Serializable序列化介面
@SuppressWarnings("serial")
public class User implements Serializable {
private String name;
private int age;
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class TestObjectStream {
public static void writeObject(Object o) {
FileOutputStream file;
try {
file = new FileOutputStream("E:\\img\\obj.dat");// 物件保存到本地.dat檔案
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(o);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object readObject(String path) {
Object obj = null;
try {
FileInputStream file = new FileInputStream(path);
ObjectInputStream in = new ObjectInputStream(file);
obj = in.readObject();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static void main(String[] args) {
// writeObject(new User("jack", 35));
User user = (User) readObject("E:\\img\\obj.dat");
System.out.println("name:" + user.getName());
System.out.println("age:" + user.getAge());
}
}
3、InputStream 和 OutputStream

4、Reader 和 Writer

5、節點流和處理流
節點流(原始流):可以從或向一個特定的地方(節點)讀寫資料,如FileReader.
處理流(鏈接流):是對一個已存在的流的連接和封裝,
通過所封裝的流的功能調用實作資料讀寫,
如BufferedReader處理流的構造方法總是要帶一個其他的流物件做引數,
一個流物件經過其他流的多次包裝,稱為流的鏈接,
6、通過URL讀取流
URL類:代表一個統一資源定位符,它是指向互聯網“資源”的指標,
public static void main(String[] args){
try{
URL url=new URL("https://127.0.0.1:8090/SoftMgr/dzh.jpg");
InputStream in=url.openStream();
FileOutputStream out=new FileOutputStream("D:\\SoftMgr\\dyw.jpg");
int len=0;
byte[] bs=new byte[1024];
while((len=in.read(bs))!=-1){
out.write(bs,0,len);
}
out.flush();
out.close();
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/316589.html
標籤:java
