欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          java中的IO流如何分類

          java中的IO流如何分類

          一、IO:上傳下載,寫入寫出

          流: 一連串流動(dòng)的數(shù)據(jù),以先入先出的方式進(jìn)行流動(dòng),管道,以程序?yàn)橹行?、程序與網(wǎng)絡(luò)|文件|服務(wù)器|數(shù)組..

          分類

          1)按照流向:

          輸入流;輸出流

          2)操作單元分:

          字節(jié)流 (萬能流):任何內(nèi)容都可以轉(zhuǎn)為字節(jié),可以傳輸文本,圖片,音頻;字符流 :只能操作純文本數(shù)據(jù)

          3)按照功能:

          節(jié)點(diǎn)流 : 包裹源頭,實(shí)現(xiàn)基本功能;功能流 : 對(duì)節(jié)點(diǎn)流增強(qiáng)性能,提高效率

          4)各個(gè)分類之間是相輔相成的

          字節(jié)流

          (1)字節(jié)輸入流 : InputStream 此抽象類是表示字節(jié)輸入流的所有類的超類;FileInputStream 從文件系統(tǒng)中的某個(gè)文件中獲得輸入字節(jié)

          //字節(jié)流輸入 InputStream  //導(dǎo)包	導(dǎo)包快捷鍵: 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)  		通過打開一個(gè)到實(shí)際文件的連接來創(chuàng)建一個(gè) FileInputStream,該文件通過文件系統(tǒng)中的路徑名 name 指定 		//FileInputStream(File file)  		通過打開一個(gè)到實(shí)際文件的連接來創(chuàng)建一個(gè) FileInputStream, 		該文件通過文件系統(tǒng)中的 File 對(duì)象 file 指定 		InputStream is=new FileInputStream(new File("D:/test.txt"));//創(chuàng)建D盤符的根目錄下的文件 		System.out.println(is); 		//方式1: int read() 讀入數(shù)據(jù) 一個(gè)字節(jié)一個(gè)字節(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ǎn)化代碼,重復(fù)代碼只寫一次,但還是一個(gè)字節(jié)一個(gè)字節(jié)的讀入 		/*int num=-1; 		while((num=is.read())!=-1){ 			System.out.println((char)num); 		}*/ 		//方式3:int read(byte[] b) 一個(gè)字節(jié)數(shù)組一個(gè)字節(jié)數(shù)組的讀入 		//返回值: 返回讀入到字節(jié)數(shù)組中數(shù)據(jù)的個(gè)數(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 //導(dǎo)包	導(dǎo)包快捷鍵: 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)建一個(gè)向具有指定名稱的文件中寫入數(shù)據(jù)的輸出文件流 		//FileOutputStream(String name,boolean append)   		創(chuàng)建一個(gè)向具有指定 name 的文件中寫入數(shù)據(jù)的輸出文件流 		//FileOutputStream(File file,boolean append)  		創(chuàng)建一個(gè)向指定 File 對(duì)象表示的文件中寫入數(shù)據(jù)的文件輸出流 		//boolean append	返回值:true追加,false不追加(默認(rèn)false) 		OutputStream os=null; 		//try...catch(){}	捕捉異常,處理異常 		try { 			//1.選擇流 			os=new FileOutputStream("D:/hhh.txt",hhtrue);  			//文件不存在,系統(tǒng)會(huì)自動(dòng)幫我們創(chuàng)建,但文件夾不會(huì) 			//2.準(zhǔn)備數(shù)據(jù) 			String str="要好好學(xué)習(xí),天天向上...";  			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 {//預(yù)防空指針異常 				if(os!=null){ 					os.close(); 				} 			} catch (IOException e) { 				e.printStackTrace(); 			} 		} 	} }

          字符流

          只能操作純文本

          1)節(jié)點(diǎn)流:

          (1)字符輸入流:Reader 讀取字符流的抽象類;FileReader 用來讀取字符文件的便捷類

          //字符輸入流 Reader //導(dǎo)包	導(dǎo)包快捷鍵: 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() 讀取單個(gè)字符。  			/*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;  //存儲(chǔ)讀到的數(shù)據(jù)  如果為-1,證明已達(dá)到末尾 			//方法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 //導(dǎo)包	導(dǎo)包快捷鍵: 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) 		//默認(rèn)不追加 		//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) 寫入單個(gè)字符 				void write(String str)  寫入字符串  				void write(String str, int off, int len) 寫入字符串的某一部分  			*/ 			rt.write(97); 			rt.write("rn");  		//換行 			rt.write("你真好看!!!!"); 			rt.write("rn"); 			rt.write("你真好看!!!!",2,2); 			rt.write("rn"); 			char[] ch={'a','b','c','d','e'}; 			rt.write(ch); 			rt.write("rn"); 			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(); 				} 			} 		} 	} }

          功能流

          緩沖流: 增強(qiáng)功能,提高性能,加快讀寫效率

          1)字節(jié)流:

          BufferedInputStream 字節(jié)輸入流緩沖流

          BufferedOutputStream 字節(jié)輸出流緩沖流

          沒有新增方法,可以發(fā)生多態(tài)使用

          //導(dǎo)包 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() 讀取一個(gè)文本行

          BufferedWriter 字符輸出流緩沖流

          新增方法: void newLine() 寫入一個(gè)行分隔符

          //導(dǎo)包 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.選擇流  導(dǎo)包快捷鍵: 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(); 	} }

          Data流(字節(jié)節(jié)點(diǎn)流)

          Data流(字節(jié)節(jié)點(diǎn)流):

          讀寫基本數(shù)據(jù)類型+String類型數(shù)據(jù),是字節(jié)流功能流的一種

          DataInputStream 新增方法: readXxx()

          DataOutputStream 新增方法: writeXxx()

          存在新增方法不能發(fā)生多態(tài),先寫出再寫入

          可能碰到的異常:EOFException 文件有,內(nèi)容讀入不到,必須讀入的是寫出的源文件

          //導(dǎo)包 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.準(zhǔn)備數(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(); 	} }

          對(duì)象流

          Object 保存數(shù)據(jù)類型+數(shù)據(jù)

          字節(jié)的功能流:當(dāng)想要傳輸|讀寫對(duì)象類型數(shù)據(jù)的時(shí)候,可以使用一個(gè)對(duì)象流

          序列化: 把對(duì)象類型的數(shù)據(jù)轉(zhuǎn)化為可存儲(chǔ)|可傳輸?shù)臓顟B(tài)的過程

          ObjectInputStream() 反序列化輸入流 新增方法: readObject()

          ObjectOutputStream() 序列化輸出流 新增方法: writeObject()

          注意:

          1)先序列化后反序列化

          2)序列化反序列讀寫順序一致

          3)不是所有的類都能序列化 java.io.Serializable 空接口

          4)不是所有的屬性都需要序列化 transient

          5)static內(nèi)容不會(huì)被序列化

          6)如果父類實(shí)現(xiàn)Serializable接口,子類中可以序列化所有內(nèi)容

          如果子類實(shí)現(xiàn)Serializable接口,但是父類沒有實(shí)現(xiàn),子類只能序列化子類獨(dú)有的內(nèi)容

          //導(dǎo)包 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.輸出對(duì)象信息 		ObjectOutputStream os=new ObjectOutputStream(new BufferedOutputStream(new  		FileOutputStream(path))); 		//2.準(zhǔn)備數(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)容

          能夠定義真實(shí)存在的路徑|文件,不在的也可以定義,所以抽象表現(xiàn)形式

          構(gòu)造器: File(File parent, String child) 根據(jù) parent 抽象路徑名和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例

          File(String pathname) 通過將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建一個(gè)新 File 實(shí)例

          File(String parent, String child) 根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例

          推薦教程:java入門教程

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)