黑馬程式設計師--java基礎--io流
阿新 • • 發佈:2019-02-07
------- android培訓、java培訓、期待與您交流! ---------
io流有四個基類:
分別為
字元流:
Reader
Writer
位元組流
InputStream
OutputStream
由這四個類派生出來的子類都以父類名作為其名字的字尾。
在java的io操作都有相應的步驟:
1.使用File類開啟一個檔案。
2.通過字元流或位元組流來指定讀取的位置。
3.進行讀取操作。
4.關閉輸入流、輸出流。
常用的字元讀取FileReader,FileWriter操作:
package com.itheima; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public class IODemo { /** * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub File file = new File("d:\\1.txt");//1.使用File類開啟一個檔案。 //2.通過字元流或位元組流來指定讀取的位置。 FileReader fileReader = new FileReader(file); File file2 = new File("D:\\coyp1.txt"); FileWriter fileWriter = new FileWriter(file2); //3.進行讀取操作。 int len = 0; while ((len = fileReader.read())!= -1) { fileWriter.write(len); } // fileReader.close(); fileWriter.close(); } }
常用的位元組讀取FileInputStream,FileOutStream操作
操作:
若是想提高io流的操作效率,我們可以建立緩衝區。package com.itheima; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; public class IODemo { /** * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub File file = new File("d:\\My Medicine.mp3");//1.使用File類開啟一個檔案。 //2.通過字元流或位元組流來指定讀取的位置。 FileInputStream fileInputStream = new FileInputStream(file); File file2 = new File("D:\\coypMy Medicine.mp3"); FileOutputStream fileOutputStream = new FileOutputStream(file2); //3.進行讀取操作。 byte[] buf = new byte[1024*4]; int len = 0; while ((len = fileInputStream.read(buf))!= -1) { fileOutputStream.write(buf, 0, len); } // fileInputStream.close(); fileOutputStream.close(); } }
例子:
package com.itheima; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; public class IODemo { /** * 緩衝區出現是為了提高流的操作效率。所以在建立緩衝區錢,必須要有流物件 */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub File file = new File("d:\\My Medicine.mp3");//1.使用File類開啟一個檔案。 //2.通過字元流或位元組流來指定讀取的位置。 FileInputStream fileInputStream = new FileInputStream(file); File file2 = new File("D:\\coypMy Medicine.mp3"); FileOutputStream fileOutputStream = new FileOutputStream(file2); //只要將需要被提高效率的流物件加入緩衝區的建構函式 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); //3.進行讀取操作。 byte[] buf = new byte[1024*4]; int len = 0; ////只要用到緩衝區,就要記得重新整理 while ((len = bufferedInputStream.read(buf))!= -1) { bufferedOutputStream.write(buf, 0, len); bufferedOutputStream.flush(); } fileInputStream.close(); fileOutputStream.close(); } }
io流一定會產生異常,我們就要對異常進行處理,丟擲異常的操作是不負責任的。
package InputOutput;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo2 {
/**
*io異常的處理方式
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileWriter fw= null;
try {
fw = new FileWriter("C:\\Users\\F4\\Desktop\\demo.txt");
fw.write("啊不錯的風格");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.toString());
} finally{
try {
//關閉輸入流輸出流都應該在finally裡執行、
if(fw != null)
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
總結流操作的規律
1.明確源和目的
源:輸入流 Reader InputStream
目的:輸出流Write OutputStream
2.明確資料是什麼.
純文字:Reader Write
不是:
InputStream OutputStream
io流物件流:
ObjectInputStream
ObjectOutputStream
被操作的物件需要實現Serializable介面
向一個檔案中寫入一個物件,可以使用物件流來實現,稱為序列化。
package com.itheima;
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
class Person implements Serializable{
public static final long serialVersionUID = 42L;
private String name;
private transient int age;
public static String contry ="cn";
Person(String name,int age,String contry){
this.name=name;
this.age=age;
this.contry = contry;
}
public String toString(){
return name + ":" + age + " :" + contry;
}
}
public class IODemo{
public static void main(String[] args) throws Exception{
writeObj();
readObj();
}
public static void readObj() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\1.java"));
Person p = (Person)ois.readObject();
System.out.println(p);
ois.close();
}
public static void writeObj() throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\1.java"));
oos.writeObject(new Person("黑馬程式設計師",20,"HZ"));
oos.close();
}
}