欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

JAVA之IO流-創(chuàng)新互聯(lián)

IO流概述

IO:輸入/輸出(Input/Output)

成都創(chuàng)新互聯(lián)公司主營南昌縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App制作,南昌縣h5微信小程序搭建,南昌縣網(wǎng)站營銷推廣歡迎南昌縣等地區(qū)企業(yè)咨詢

流:流是一種抽象概念,是對數(shù)據(jù)傳輸?shù)目偡Q.即數(shù)據(jù)在設(shè)備間的傳輸稱為流,流的本質(zhì)是數(shù)據(jù)傳輸

流是一種抽象概念,它代表了數(shù)據(jù)的無結(jié)構(gòu)化傳遞。按照流的方式進行輸入輸出,數(shù)據(jù)被當成無結(jié)構(gòu)的字節(jié)序或字符序列。從流中取得數(shù)據(jù)的操作稱為提取操作,而向流中添加數(shù)據(jù)的操作稱為插入操作。用來進行輸入輸出操作的流就稱為IO流。換句話說,IO流就是以流的方式進行輸入輸出?。

IO流是用來處理設(shè)備間數(shù)據(jù)傳輸問題的

IO流分類?

按照數(shù)據(jù)的流向:

輸入流:讀數(shù)據(jù)

輸出流:寫數(shù)據(jù)

按照數(shù)據(jù)類型:

? 字節(jié)流:字節(jié)輸入流,字節(jié)輸出流

? 字符流:字符輸入流,字符輸出流

一般來說,IO流的分類是按照數(shù)據(jù)類型來分的

字節(jié)流?

647e86fa3e1542b7a66e617542c755c0.jpg

字節(jié)流讀數(shù)據(jù):

? int read() :一次讀取一個字節(jié)

? int read( byte[ ] bys) :一次讀取一個字節(jié)數(shù)組

字節(jié)流寫數(shù)據(jù):

? void write( int by) :一次寫一個字節(jié)

? void write( byte[ ] bys) :一次寫一個字節(jié)數(shù)組

? void write( byte[ ] bys,int index,int len) :一次寫一個字節(jié)數(shù)組的一部分

釋放資源:

? void close( ) :關(guān)閉流

小結(jié):字節(jié)流可以復(fù)制任意文件數(shù)據(jù),有4種方式,一般采用字節(jié)緩沖流一次讀寫一個字節(jié)數(shù)組的方式

//方法1
FileInputStream fis = new FileInputStream("文件路徑名");
FileOutputStream fos = new FileOutputStream("文件路徑名");
		
int ch;
while((ch = fis.read()) != -1) {
	fos.write(ch);
}
		
fis.close();
fos.close();


//方法2
FileInputStream fis = new FileInputStream("文件路徑名");
FileOutputStream fos = new FileOutputStream("文件路徑名");
		
byte[] bys = new byte[1024];
int len;
while((len = fis.read(bys)) != -1) {
	fos.write(bys, 0, len);
}
		
fis.close();
fos.close();


//方法3
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件路徑名"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("文件路徑名"));
		
int ch;
while((ch = bis.read()) != -1) {
	bos.write(ch);
}
		
bis.close();
bos.close();


//方法4
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件路徑名"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("文件路徑名"));
		
byte[] bys = new byte[1024];
int len;
while((len = bis.read(bys)) != -1) {
	bos.write(bys, 0, len);
}
		
bis.close();
bos.close();
字符流?

645ea7deb41a4e50aa950e3b5bef970a.jpg

字符流讀數(shù)據(jù):

? int read() :一次讀取一個字符

? int read( char[ ] chs) :一次讀取一個字符數(shù)組

? String readLine( ) :一次讀取一行,不包含任何終止字符(字符緩沖流特有)

字符流寫數(shù)據(jù):

? void write( int ch) :一次寫一個字符

? void write( char[ ] chs) :一次寫一個字符數(shù)組

? void write( char[ ] chs,int index,int len) :一次寫一個字符數(shù)組的一部分

? void flush( ) :刷新流,刷新流后,數(shù)據(jù)才會寫入

? void newLine( ) :寫一個換行符(字符緩沖流特有)

? void write( String line) :一次寫一個字符串(字符緩沖流特有)

釋放資源:

? void close( ) :關(guān)閉流,關(guān)閉時先刷新流,再關(guān)閉流

? 注:flush刷新緩沖后可以繼續(xù)寫入數(shù)據(jù),close刷新緩沖后關(guān)閉流,不能再寫入數(shù)據(jù)

小結(jié):字符流只能復(fù)制文本文件,有5種方式,一般采用字符緩沖流的特有功能

//字符緩沖流寫入數(shù)據(jù)
BufferedWriter bw = new BufferedWriter(new FileWriter("文件路徑名"));
		
for(int i = 0;i<5;i++) {
	bw.write("hello"+i);
	bw.newLine();
	bw.flush();
}
		
bw.close();

//字符緩沖流讀取數(shù)據(jù)
BufferedReader br = new BufferedReader(new FileReader("文件路徑名"));

String line;
while((line = br.readLine()) != null) {
	System.out.println(line);
}
		
br.close();
遞歸

程序調(diào)用自身的編程技巧稱為遞歸,一般來說,遞歸需要有邊界條件、遞歸前進段和遞歸返回段。當邊界條件不滿足時,遞歸前進;當邊界條件滿足時,遞歸返回。

遞歸遍歷目錄示例:

public static void main(String[] args) {

	File f = new File("Z:\\Learn\\Learn2");
	getFilesPath(f);
}

public static void getFilesPath(File file) {//遍歷目錄得到所有文件的絕對路徑
	File[] files = file.listFiles();
	if (files != null) {
		for (File f : files) {
			if (f.isDirectory()) {
				getFilesPath(f);
			} else {
				System.out.println(f.getAbsolutePath());
			}
		}
	}
}
字符串編碼解碼

String str = "中國";
		
        //平臺默認字符集編碼
		byte[] bys = str.getBytes();//[-28, -72, -83, -27, -101, -67]
		
        //UTF-8編碼
//		byte[] bys = str.getBytes("UTF-8");//[-28, -72, -83, -27, -101, -67]
		
        //GBK編碼
//		byte[] bys = str.getBytes("GBK");//[-42, -48, -71, -6]
		
		System.out.println(Arrays.toString(bys));//輸出字符串編碼后生成的字節(jié)數(shù)組數(shù)據(jù)
		
        //平臺默認解碼
		String s = new String(bys);
		
        //指定字符集解碼
//		String s = new String(bys,"UTF-8");
		
//		String s = new String(bys,"GBK");
		
		System.out.println(s);//輸出解碼后的字符串

注:字符流為簡化使用FileReader和FileWriter,但若涉及到編碼解碼問題,還需使用InputStreamReader和OutputStreamWriter

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

本文標題:JAVA之IO流-創(chuàng)新互聯(lián)
當前鏈接:http://chinadenli.net/article6/cdodog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作微信小程序標簽優(yōu)化搜索引擎優(yōu)化域名注冊定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名