国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 Java Java入門 java中的IO流如何分類

java中的IO流如何分類

Nov 23, 2019 pm 06:14 PM
io java 分類

java中的IO流如何分類

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

# 流: 一連流媒體的數(shù)據(jù),以先入先出的方式進行流動,管道,以程序為中心、程序與網絡|文件|伺服器|數(shù)組..(相關課程推薦:java視頻教程

分類

##1)依照流向:

輸入流;輸出流

2)操作單元分:

位元組流(萬能流):任何內容都可以轉為位元組,可以傳輸文字,圖片,音訊;字元流:只能操作純文字資料

3) 依照功能:

節(jié)點流: 包裹來源,實現(xiàn)基本功能;功能流: 對節(jié)點流增強效能,提高效率

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

#位元組流

(1)位元組輸入流: InputStream 此抽象類別是表示位元組輸入流的所有類別的超類別;FileInputStream 從檔案系統(tǒng)中的某個檔案中獲得輸入位元組

//字節(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));
		}
		//關閉
		is.close();
	}
}

(2)位元組輸出流: OutputStream此抽象類別是表示輸出位元組流的所有類別的超類別;FileOutputStream? 檔案輸出流是用於將資料寫入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.關閉
			try {//預防空指針異常
				if(os!=null){
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

字元流

#只能操作純文字

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 {
					//關閉功能
					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.關閉
			if(null!=rt){
				try {
					rt.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

功能流程

緩衝流: 增強功能,提高效能,加快讀寫效率

1)位元組流:

BufferedInputStream 位元組輸入流緩衝流

# BufferedOutputStream 位元組輸出流緩衝流

沒有新增方法,可以發(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.關閉
		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.關閉
		bw.close();
		br.close();
	}
}

Data流(字節(jié)節(jié)點流)

Data流(字節(jié)節(jié)點流):

讀寫基本資料類型String類型數(shù)據(jù),是位元組流功能流的一種

DataInputStream 新增方法: readXxx()

DataOutputStream 新增方法: writeXxx()

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

可能碰到的例外:EOFException 檔案有,內容讀入不到,必須讀入的是寫出的來源檔案

//導包
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 {
		//調用方法
		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.關閉
		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.關閉
		out.close();
	}
}

#物件流程

Object 儲存資料型別資料

位元組的功能流:當想要傳輸|讀寫物件類型資料的時候,可以使用一個物件流

序列化: 將物件類型的資料轉換成可儲存|可傳輸?shù)臓顟B(tài)的過程

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

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

注意:

1)先序列化後反序列化

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

3)不是所有的類別都能序列化 java.io.Serializable 空介面

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

5)static內容不會被序列化
6)如果父類別實作Serializable介面,子類別中可以序列化所有內容

###如果子類別實作Serializable介面,但是父類別沒有實作,子類只能序列化子類別獨有的內容###
//導包
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,關閉
		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.關閉
		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 類別:檔案與目錄路徑名稱的抽象表示形式##### # 提供的功能:操作檔案外部的能力,無法操作檔案內部的內容###### 能夠定義真實存在的路徑|檔案,不在的也可以定義,所以抽象表現(xiàn)形式####### 建構器: File(File parent, String child)? ?根據(jù)parent 抽象路徑名稱和child 路徑名稱字串建立一個新File 實例?###### ? File(String pathname)? 透過將給定路徑名稱字串轉換為抽象路徑名稱來建立一個新File 實例?###### File(String parent, String child)? ?根據(jù)parent 路徑名稱字串和child 路徑名稱字串建立一個新File 實例?#######本文來自###java入門教學# ##欄目,歡迎學習! ######

以上是java中的IO流如何分類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1600
29
PHP教程
1501
276
如何使用JDBC處理Java的交易? 如何使用JDBC處理Java的交易? Aug 02, 2025 pm 12:29 PM

要正確處理JDBC事務,必須先關閉自動提交模式,再執(zhí)行多個操作,最後根據(jù)結果提交或回滾;1.調用conn.setAutoCommit(false)以開始事務;2.執(zhí)行多個SQL操作,如INSERT和UPDATE;3.若所有操作成功則調用conn.commit(),若發(fā)生異常則調用conn.rollback()確保數(shù)據(jù)一致性;同時應使用try-with-resources管理資源,妥善處理異常並關閉連接,避免連接洩漏;此外建議使用連接池、設置保存點實現(xiàn)部分回滾,並保持事務盡可能短以提升性能。

了解Java虛擬機(JVM)內部 了解Java虛擬機(JVM)內部 Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

如何使用Java的日曆? 如何使用Java的日曆? Aug 02, 2025 am 02:38 AM

使用java.time包中的類替代舊的Date和Calendar類;2.通過LocalDate、LocalDateTime和LocalTime獲取當前日期時間;3.使用of()方法創(chuàng)建特定日期時間;4.利用plus/minus方法不可變地增減時間;5.使用ZonedDateTime和ZoneId處理時區(qū);6.通過DateTimeFormatter格式化和解析日期字符串;7.必要時通過Instant與舊日期類型兼容;現(xiàn)代Java中日期處理應優(yōu)先使用java.timeAPI,它提供了清晰、不可變且線

比較Java框架:Spring Boot vs Quarkus vs Micronaut 比較Java框架:Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

前形式攝取,quarkusandmicronautleaddueTocile timeProcessingandGraalvSupport,withquarkusoftenpernperforminglightbetterine nosserless notelless centarios.2。

垃圾收集如何在Java工作? 垃圾收集如何在Java工作? Aug 02, 2025 pm 01:55 PM

Java的垃圾回收(GC)是自動管理內存的機制,通過回收不可達對象釋放堆內存,減少內存洩漏風險。 1.GC從根對象(如棧變量、活動線程、靜態(tài)字段等)出發(fā)判斷對象可達性,無法到達的對像被標記為垃圾。 2.基於標記-清除算法,標記所有可達對象,清除未標記對象。 3.採用分代收集策略:新生代(Eden、S0、S1)頻繁執(zhí)行MinorGC;老年代執(zhí)行較少但耗時較長的MajorGC;Metaspace存儲類元數(shù)據(jù)。 4.JVM提供多種GC器:SerialGC適用於小型應用;ParallelGC提升吞吐量;CMS降

了解網絡端口和防火牆 了解網絡端口和防火牆 Aug 01, 2025 am 06:40 AM

NetworkPortSandFireWallsworkTogetHertoEnableCommunication whereSeringSecurity.1.NetWorkPortSareVirtualendPointSnumbered0-655 35,with-Well-with-Newonportslike80(HTTP),443(https),22(SSH)和25(smtp)sindiessingspefificservices.2.portsoperateervertcp(可靠,c

以身作則,解釋說明 以身作則,解釋說明 Aug 02, 2025 am 06:26 AM

defer用於在函數(shù)返回前執(zhí)行指定操作,如清理資源;參數(shù)在defer時立即求值,函數(shù)按後進先出(LIFO)順序執(zhí)行;1.多個defer按聲明逆序執(zhí)行;2.常用於文件關閉等安全清理;3.可修改命名返回值;4.即使發(fā)生panic也會執(zhí)行,適合用於recover;5.避免在循環(huán)中濫用defer,防止資源洩漏;正確使用可提升代碼安全性和可讀性。

比較Java構建工具:Maven vs. Gradle 比較Java構建工具:Maven vs. Gradle Aug 03, 2025 pm 01:36 PM

Gradleisthebetterchoiceformostnewprojectsduetoitssuperiorflexibility,performance,andmoderntoolingsupport.1.Gradle’sGroovy/KotlinDSLismoreconciseandexpressivethanMaven’sverboseXML.2.GradleoutperformsMaveninbuildspeedwithincrementalcompilation,buildcac

See all articles