使用io的目的是想要 讀寫檔案内部的内容,讀寫的方案就是流,既建立管道,讓資料以流動的形式,先入先出的方式進行傳輸;
資料從資料源流向目的地;
java提供了IO包供使用者使用;
流的分類
流向分: (以程式為中心)
輸入流
輸出流
操作單元分:
位元組流:任何資料都可以傳輸
字元流: 文本
功能分:
節點流: 操作基本功能的,實作讀入寫出的,資料從資料源頭到目的地的
功能流: 增強功能,提高性能,操作與節點流之上的
節點流:
1.位元組流 :任意類型的資料都能讀寫
位元組輸入流
InputStream
位元組輸出流
OutputSream
位元組輸入流的方法:
位元組輸出流的方法:
位元組輸入流 InputStream例:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class IODemo01 {
public static void main(String[] args) throws IOException {
//1.建立聯系
File file=new File("D:/haha.txt");
//2.選擇流
//FileInputStream is=new FileInputStream(file);
FileInputStream is=new FileInputStream("D:/haha.txt");
//3.讀
//read() 讀取一個位元組資料 傳回讀取到的位元組數|沒讀到的-1
int num=is.read();
System.out.println((char)num);
int num2=is.read();
System.out.println((char)num2);
int num3=is.read();
System.out.println((char)num3);
System.out.println(is.read());
//4.關閉
is.close();
}
}
位元組輸出流 OutputSream例:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class IODemo04 {
public static void main(String[] args) throws IOException {
//注意:目标檔案如果不存在,系統會自動為你建立,但是目錄不會
//1.選擇流 boolean append ->true追加 ,false->不追加覆寫
OutputStream os=new FileOutputStream("D:/lalala.txt",true);
//2.寫出
byte[] str="不吃飯修仙哈哈哈".getBytes();
// os.write(str);
os.write(str,0,3);
//3.刷出
os.flush();
//4.關閉
os.close();
}
}
使用位元組流實作輸入輸出的時候,我們就可以實作檔案的拷貝,将檔案轉換為位元組的形式然後以位元組的形式讀取出來,實作檔案的還原
位元組流檔案拷貝:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyUtils {
public static void main(String[] args) {
copyFile("D:/haha.txt","D:/hehe.txt");
}
//檔案拷貝方法的重載用來接收字元串形式的檔案絕對路徑
public static void copyFile(String src,String dest){
copyFile(new File(src),new File(dest));
}
//檔案的拷貝 傳回值:沒有 參數:資料源路徑 目的地路徑
public static void copyFile(File src,File dest){
//1.選擇流
InputStream is = null;
OutputStream os= null;
try {
is = new FileInputStream(src);
os= new FileOutputStream(dest);
//2.讀寫
byte[] car= new byte[1024];
int len = -1; //讀入到數組中的位元組個數
while((len=is.read(car))!=-1){
os.write(car, 0, len);
}
//3.刷出
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//4.後打開的先關閉
try {
if(os!=null){
os.close();
}
if(is!=null){
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2.字元流 :隻度寫純文字
字元輸入流: Reader FileReader
字元輸出流: Writer FileWriter
例:
public class CharIO01 {
public static void main(String[] args) throws IOException {
//1.選擇流
Reader rd=new FileReader("D:/haha.txt");
Writer rt=new FileWriter("D:/cc.txt");
//2.讀寫
char[] car=new char[1024];
int len=-1; //存儲資料的個數
while((len=rd.read(car))!=-1){
rt.write(car, 0, len);
}
//3.刷出
rt.flush();
//4.關閉
rt.close();
rd.close();
}
}
功能流:作用與節點流
3.緩沖流:增加功能,提高性能,提高讀寫效率
位元組緩沖輸入流:
BufferedInputStream
位元組緩沖輸出流:
BufferedOutputStream
字元緩沖輸入流:
BufferedReader
字元緩沖輸出流:
BufferedWriter
位元組緩沖輸入流:
BufferedInputStream
方法:
位元組緩沖輸出流:BufferedOutputStream
方法:
例:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class BufferedDemo01 {
public static void main(String[] args) throws IOException {
//1.流
InputStream is = new BufferedInputStream(new FileInputStream("D:/haha.txt"));
OutputStream os=new BufferedOutputStream( new FileOutputStream("D:/dd.txt"));
//2.讀寫
byte[] car = new byte[1024];
int len=-1;
while(-1!=(len=is.read(car))){
os.write(car,0,len);
}
//3.刷出
os.flush();
//4.關閉
os.close();
is.close();
}
}
字元緩沖輸入流:BufferedReader
方法:
字元緩沖輸出流:BufferedWriter
方法:
例:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedDemo02 {
public static void main(String[] args) throws IOException {
//1.選擇流
BufferedReader rd=new BufferedReader(new FileReader("D:/haha.txt"));
BufferedWriter rt=new BufferedWriter(new FileWriter("D:/aaa.txt"));
//2.讀入
String msg=null;
while((msg = rd.readLine())!=null){
rt.write(msg);
rt.newLine(); //換行
}
//3.刷出
rt.flush();
//4.關閉
rt.close();
rd.close();
}
}
4.轉換流:位元組轉字元
位元組轉換字元輸入流:InputStreamReader
位元組轉換字元輸出流:OutputStreamWriter
位元組轉換字元輸入流:InputStreamReader
方法:
位元組轉換字元輸出流:OutputStreamWriter
方法:
例:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
public class ChangeDemo02 {
public static void main(String[] args) throws IOException {
//1.選擇流
BufferedReader rd=new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream("D:/haha.txt"))));
BufferedWriter rt=new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("D:/bbb.txt"))));
String msg=null;
//2.讀入寫出
while((msg=rd.readLine())!=null){
rt.write(msg);
rt.newLine();
}
//3.刷出
rt.flush();
//4.關閉
rt.close();
}
}
5.Data流:基本資料類型|String+資料 :資料輸入流允許應用程式以與機器無關方式從底層輸入流中讀取基本 Java 資料類型
DataInputStream
DataOutputStream
執行個體:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 基本資料類型流:位元組流的功能流
* 讀寫帶有基本資料類型|字元串類型的資料
* DataInputStream
* 新增方法: ReadXxx()
* DataOutputStream
* 新增方法: writeXxx()
* 不能發生多态
*/
public class DataDemo03 {
public static void main(String[] args) throws IOException {
write("D:/eee.txt");
read("D:/eee.txt");
}
//寫入
public static void read(String src) throws IOException{
//1.輸入流
DataInputStream in=new DataInputStream(new FileInputStream(src));
//2.讀入 讀入和寫出的順序要保持一緻
boolean b=in.readBoolean();
int i=in.readInt();
String s=in.readUTF();
System.out.println(b+"-->"+i+"-->"+s);
//3.關閉
in.close();
}
//寫出
public static void write(String dest) throws IOException{
//1.輸出流
DataOutputStream out=new DataOutputStream(new FileOutputStream(dest));
//2.準備資料
boolean flag=false;
int i=100;
String s="哈哈";
//3.寫出
out.writeBoolean(flag);
out.writeInt(i);
out.writeUTF(s);
//4.刷出
out.flush();
//5.關閉
out.close();
}
}
6.對象流:任意類型,包括對象+資料:一個ObjectInputStream反序列化的原始資料和對象之前使用一個ObjectOutputStream寫的
ObjectInputStream
ObjectOutputStream
對象流:資料+資料類型
序列化和反序列化:序列化 (Serialization)是将對象的狀态資訊轉換為可以存儲或傳輸的形式的過程
序列化輸出流 : ObjectOutputStream
反序列化輸入流: ObjectInputStream
先序列化後反序列化
不是所有的類都能序列化 實作java.io.Serializable接口
不是所有的屬性都需要序列化 transient
靜态的内容不能序列化
如果父類有實作序列化,子類沒有,子類中所有的内容都能序列化
如果父類中沒有實作序列化,子類有實作序列化,子類隻能序列化自己的内容
執行個體:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
public class ObjectDemo04 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
write();
read();
}
//序列化輸出
public static void read() throws FileNotFoundException, IOException, ClassNotFoundException{
//1.流
ObjectInputStream is=new ObjectInputStream(new FileInputStream("D:/object.txt"));
//2.讀入寫出讀入的順序保持一緻
Object p=is.readObject();
System.out.println(p);
if(p instanceof Person){
Person p1=(Person)p;
System.out.println(p1);
}
System.out.println(Arrays.toString((int[])is.readObject()));
//3.關閉
is.close();
}
//序列化輸出
public static void write() throws FileNotFoundException, IOException{
//1.流
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("D:/object.txt"));
//2.寫出
Person p=new Person("lisi",20);
int[] arr=new int[]{1,2,3,4,5};
//寫出讀入的順序保持一緻
out.writeObject(p);
out.writeObject(arr);
//3.刷出
out.flush();
//4.關閉
out.close();
p.setAge(100);
p.setName("王五");
}
}
class Person implements Serializable{
private String name;
// private transient int age;
private static int age;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}