一、IO流
IO:上傳下載,寫入寫出
流: 一連串流動的數(shù)據(jù),以先入先出的方式進行流動,管道,以程序為中心、程序與網(wǎng)絡(luò)|文件|服務器|數(shù)組..
1.分類
1)按照流向:
輸入流:
輸出流:
2)操作單元分:
字節(jié)流 (萬能流):任何內(nèi)容都可以轉(zhuǎn)為字節(jié),可以傳輸文本,圖片,音頻...
字符流 :只能操作純文本數(shù)據(jù)
3)按照功能:
節(jié)點流 : 包裹源頭,實現(xiàn)基本功能
功能流 : 對節(jié)點流增強性能,提高效率
4)各個分類之間是相輔相成的
2.字節(jié)流:
1)節(jié)點流
(1)字節(jié)輸入流 : InputStream
此抽象類是表示字節(jié)輸入流的所有類的超類
FileInputStream
從文件系統(tǒng)中的某個文件中獲得輸入字節(jié)
//字節(jié)流輸入 InputStream
//導包
導包快捷鍵: ctrl+shift+o
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Demo01 {
public static void main(String[] args) throws IOException {
//FileInputStream(String name) 通過打開一個到實際文件的連接來創(chuàng)建一個 FileInputStream,該文件通過文件系統(tǒng)中的路徑名 name 指定
//FileInputStream(File file) 通過打開一個到實際文件的連接來創(chuàng)建一個 FileInputStream,該文件通過文件系統(tǒng)中的 File 對象 file 指定
InputStream is=new FileInputStream(new File("D:/test.txt"));//創(chuàng)建D盤符的根目錄下的文件
System.out.println(is);
//方式1: int read() 讀入數(shù)據(jù) 一個字節(jié)一個字節(jié)讀入
/*int num=is.read();
System.out.println((char)num);
num=is.read();
System.out.println((char)num);
System.out.println((char)(is.read()));*/
//方式2:使用循環(huán),循環(huán)讀入,可以簡化代碼,重復代碼只寫一次,但還是一個字節(jié)一個字節(jié)的讀入
/*int num=-1;
while((num=is.read())!=-1){
System.out.println((char)num);
}*/
//方式3:int read(byte[] b) 一個字節(jié)數(shù)組一個字節(jié)數(shù)組的讀入
//返回值: 返回讀入到字節(jié)數(shù)組中數(shù)據(jù)的個數(shù),沒有讀到返回-1
byte[] car=new byte[1];
//int len=is.read(car);
int len=-1;
while((len=is.read(car))!=-1){
System.out.println(new String(car,0,len));
}
//關(guān)閉
is.close();
}
}
(2)字節(jié)輸出流: OutputStream
此抽象類是表示輸出字節(jié)流的所有類的超類
FileOutputStream
文件輸出流是用于將數(shù)據(jù)寫入 File 的輸出流
//字節(jié)輸出流 OutputStream
//導包
導包快捷鍵: ctrl+shift+o
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Demo02 {
public static void main(String[] args) {
//FileOutputStream(String name)創(chuàng)建一個向具有指定名稱的文件中寫入數(shù)據(jù)的輸出文件流
//FileOutputStream(String name,boolean append) 創(chuàng)建一個向具有指定 name 的文件中寫入數(shù)據(jù)的輸出文件流
//FileOutputStream(File file,boolean append) 創(chuàng)建一個向指定 File 對象表示的文件中寫入數(shù)據(jù)的文件輸出流
//boolean append
返回值:true追加,false不追加(默認false)
OutputStream os=null;
//try...catch(){}
捕捉異常,處理異常
try {
//1.選擇流
os=new FileOutputStream("D:/hhh.txt",hhtrue); //文件不存在,系統(tǒng)會自動幫我們創(chuàng)建,但文件夾不會
//2.準備數(shù)據(jù)
String str="要好好學習,天天向上...";
byte[] c和=str.getBytes();
//3.寫出 void write(byte[] b)
os.write(ch);
//4.刷出
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
//5.關(guān)閉
try {//預防空指針異常
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.字符流:
只能操作純文本
1)節(jié)點流:
(1)字符輸入流:Reader
讀取字符流的抽象類
FileReader
用來讀取字符文件的便捷類
//字符輸入流 Reader
//導包
導包快捷鍵: ctrl+shift+o
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Demo03 {
public static void main(String[] args) {
//FileReader(String fileName)
Reader rd=null;
try {
//1.選擇流
rd=new FileReader("D:/hehe.txt");//創(chuàng)建D盤符的根目錄下的文件
//2.讀入
//方法1:int read() 讀取單個字符。
/*System.out.println((char)rd.read());
System.out.println((char)rd.read());
System.out.println((char)rd.read());
System.out.println((char)rd.read());*/
int len=-1; //存儲讀到的數(shù)據(jù) 如果為-1,證明已達到末尾
//方法2:
/*while(-1!=(len=rd.read())){
System.out.println((char)len);
}*/
//方法3:int read(char[] cbuf) 將字符讀入數(shù)組。
char[] car=new char[1024];
while((len=rd.read(car))!=-1){
System.out.println(new String(car,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(rd!=null){
try {
//關(guān)閉功能
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(2)字符輸出流: Writer
寫入字符流的抽象類
FileWriter
用來寫入字符文件的便捷類
//字符輸出流:Writer
//導包
導包快捷鍵: ctrl+shift+o
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class CharDemo02 {
public static void main(String[] args) {
//FileWriter(File file)
//默認不追加
//FileWriter(File file, boolean append)
//FileWriter(String file)
//FileWriter(String file, boolean append)
Writer rt=null;
try {
//1.選擇流
rt=new FileWriter("D:/houhou.txt",true);
//2.寫出
/*
void write(char[] cbuf) 寫入字符數(shù)組
void write(char[] cbuf, int off, int len) 寫入字符數(shù)組的某一部分
void write(int c) 寫入單個字符
void write(String str) 寫入字符串
void write(String str, int off, int len) 寫入字符串的某一部分
*/
rt.write(97);
rt.write("\r\n");
//換行
rt.write("你真好看!!!!");
rt.write("\r\n");
rt.write("你真好看!!!!",2,2);
rt.write("\r\n");
char[] ch={'a','b','c','d','e'};
rt.write(ch);
rt.write("\r\n");
rt.write(ch,2,3);
//3.刷出
rt.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
//4.關(guān)閉
if(null!=rt){
try {
rt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4.功能流(節(jié)點流):
緩沖流: 增強功能,提高性能,加快讀寫效率
1)字節(jié)流:
BufferedInputStream
字節(jié)輸入流緩沖流
BufferedOutputStream
字節(jié)輸出流緩沖流
沒有新增方法,可以發(fā)生多態(tài)使用
//導包
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 BufferedInputStream01 {
public static void main(String[] args) throws IOException {
//1.選擇流
//BufferedInputStream(InputStream in)
InputStream is=new BufferedInputStream(new FileInputStream("D:/hehe.txt"));
OutputStream os=new BufferedOutputStream(new FileOutputStream("E:/hengheng.txt") );
//2.讀寫
byte[] car=new byte[1024];
int len=-1;
while((len=is.read(car))!=-1){
os.write(car,0,len);
}
//3.刷出
os.flush();
//4.關(guān)閉
os.close();
is.close();
}
}
2)字符流:
BufferedReader
字符輸入流緩沖流
新增方法:
String readLine()
讀取一個文本行
BufferedWriter
字符輸出流緩沖流
新增方法:
void newLine()
寫入一個行分隔符
//導包
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedReader02 {
public static void main(String[] args) throws IOException {
//1.選擇流 導包快捷鍵: ctrl+shift+o
BufferedReader br=new BufferedReader(new FileReader("D:/hehe.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("D:/ccc.txt"));
//2.讀寫
String msg=null;
while((msg=br.readLine())!=null){
bw.write(msg);
bw.newLine();
}
//3.刷出
bw.flush();
//4.關(guān)閉
bw.close();
br.close();
}
}
5.Data流(字節(jié)節(jié)點流)
Data流(字節(jié)節(jié)點流):
讀寫基本數(shù)據(jù)類型+String類型數(shù)據(jù),是字節(jié)流功能流的一種
DataInputStream
新增方法: readXxx()
DataOutputStream
新增方法: writeXxx()
存在新增方法不能發(fā)生多態(tài),先寫出再寫入
可能碰到的異常:EOFException 文件有,內(nèi)容讀入不到,必須讀入的是寫出的源文件
//導包
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Data01 {
public static void main(String[] args) throws IOException {
//調(diào)用方法
write("D:/data.txt");
read("D:/data1.txt");
}
//讀入
public static void read(String path) throws IOException{
//1.輸入流 數(shù)據(jù)類型+數(shù)據(jù)
DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
//2.讀入
int i=in.readInt();
boolean b=in.readBoolean();
String s=in.readUTF();
System.out.println(i+"-->"+b+"-->"+s);
//3.關(guān)閉
in.close();
}
//寫出
public static void write(String path) throws IOException{
//1.輸出流
DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
//2.準備數(shù)據(jù)
int i=101;
boolean f=false;
String s="哈哈";
//3.寫出 寫出和讀入的順序要保持一致
out.writeInt(i);
out.writeBoolean(f);
out.writeUTF(s);
//4.刷出
out.flush();
//5.關(guān)閉
out.close();
}
}
6.對象流:
Object 保存數(shù)據(jù)類型+數(shù)據(jù)
字節(jié)的功能流:當想要傳輸|讀寫對象類型數(shù)據(jù)的時候,可以使用一個對象流
序列化: 把對象類型的數(shù)據(jù)轉(zhuǎn)化為可存儲|可傳輸?shù)臓顟B(tài)的過程
ObjectInputStream() 反序列化輸入流 新增方法: readObject()
ObjectOutputStream() 序列化輸出流 新增方法: writeObject()
注意:
1)先序列化后反序列化
2)序列化反序列讀寫順序一致
3)不是所有的類都能序列化
java.io.Serializable 空接口
4)不是所有的屬性都需要序列化
transient
5)static內(nèi)容不會被序列化
6)如果父類實現(xiàn)Serializable接口,子類中可以序列化所有內(nèi)容
如果子類實現(xiàn)Serializable接口,但是父類沒有實現(xiàn),子類只能序列化子類獨有的內(nèi)容
//導包
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
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 ObjectDemo02 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
write("D:/object.txt");
read("D:/object.txt");
}
//反序列化輸入
public static void read(String path) throws IOException, ClassNotFoundException{
//1.輸入流
ObjectInputStream is=new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));
//2.讀入
Object p= is.readObject();
int[] arr= (int[]) is.readObject();
if(p instanceof Person){
Person person=(Person)p;
System.out.println(person.getName());
}
System.out.println(p);
System.out.println(Arrays.toString(arr));
//3,關(guān)閉
is.close();
}
//序列化輸出
public static void write(String path) throws IOException{
//1.輸出對象信息
ObjectOutputStream os=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
//2.準備數(shù)據(jù)
Person p=new Person("aaa",18);
int[] arr={1,2,3,4};
//3.輸出
os.writeObject(p);
os.writeObject(arr);
//4.刷出
os.flush();
//5.關(guān)閉
os.close();
}
}
//接口
class Person implements Serializable{
private String name;
private static int age;
public Person() {
}
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 + "]";
}
}
二、File 類
File 類:文件和目錄路徑名的抽象表示形式
提供的功能:操作文件外部的能力,不能操作文件內(nèi)部的內(nèi)容
能夠定義真實存在的路徑|文件,不在的也可以定義,所以抽象表現(xiàn)形式
構(gòu)造器:
File(File parent, String child) 根據(jù) parent 抽象路徑名和 child 路徑名字符串創(chuàng)建一個新 File 實例
File(String pathname) 通過將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建一個新 File 實例
File(String parent, String child) 根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個新 File 實例
本站文章版權(quán)歸原作者及原出處所有 。內(nèi)容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構(gòu)成任何投資及應用建議。本站是一個個人學習交流的平臺,網(wǎng)站上部分文章為轉(zhuǎn)載,并不用于任何商業(yè)目的,我們已經(jīng)盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯(lián)系我們,我們將根據(jù)著作權(quán)人的要求,立即更正或者刪除有關(guān)內(nèi)容。本站擁有對此聲明的最終解釋權(quán)。