Educoder–Java高級特性(第一章)- IO流【筆記+參考代碼】

Educoder–Java高級特性(第一章)- IO流【筆記+參考代碼】


第一關


1、下列關於字節和字符的說法正確的是(BC)
A、字節 = 字符 + 編碼
B、字符 = 字節 + 編碼
C、字節 = 字符 + 解碼
D、字符 = 字節 + 解碼

2、下列描述正確的是:(C )
A、使用代碼讀取一個文件的數據時,應該使用輸出流。
B、使用代碼複製文件的時候,只需要使用輸出流。
C、使用代碼讀取一個文本文件的數據時,只需要使用輸入流即可。
D、從客戶端向服務端發送數據可以使用輸入流。


第二關


編程要求
請仔細閱讀右側代碼,根據方法內的提示,在Begin - End區域內進行代碼補充,具體任務如下:

讀取src/step2/input/目錄下的task.txt文件信息並輸出到控制檯,使用Java代碼將字符串learning
p\fractice寫入到src/step2/output/目錄下的output.txt,若文件目錄不存在,則創建該目錄。
注意:臨時字節數組需要定義長度爲8位,否則會有空格。


測試說明
補充完代碼後,點擊測評,平臺會對你編寫的代碼進行測試,當你的結果與預期輸出一致時,即爲通過。



參考代碼

package step2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Task {
	
	public void task() throws IOException{
		/********* Begin *********/
		File file = new File("src/step2/input/task.txt");
		FileInputStream fs = new FileInputStream(file);  //定義一個文件輸入流
		byte[] b = new byte[8];                         //定義一個字節數組
		fs.read(b);                                     //將輸入流的數據讀入到字節數組
		String str = new String(b,"UTF-8");             //通過utf-8編碼表將字節轉換成字符
		System.out.println(str);

		File dir = new File("src/step2/output");
		if(!dir.exists()){
			dir.mkdir();
		}
		FileOutputStream out = new FileOutputStream("src/step2/output/output.txt"); //創建輸出流
		String str1 = "learning practice";
		byte[] c = str1.getBytes();   //將字符串轉換成字節
		out.write(c);                 //寫數據
		out.flush();                  //刷新緩存區數據
		fs.close();					  //釋放資源
		out.close();
		/********* End *********/
	}
}



第三關


編程要求
請仔細閱讀右側代碼,根據方法內的提示,在Begin - End區域內進行代碼補充,具體任務如下:

將src/step3/input/目錄下的input.txt文件複製到src/step3/output/目錄下;

複製的新文件命名爲output.txt;

input.txt文件中只有8個字符。


測試說明
補充完代碼後,點擊測評,平臺會對你編寫的代碼進行測試,當你的結果與預期輸出一致時,即爲通過。



參考代碼

package step3;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Task {
	
	public void task() throws IOException{
		/********* Begin *********/
		String file1 = "src/step3/input/input.txt";
		FileReader fr = new FileReader(file1);
		char[] ch = new char[8];
		fr.read(ch);

		String file2 = "src/step3/output/output.txt";
		FileWriter fw = new FileWriter(file2);
		fw.write(ch);
		
		fr.close();
		fw.flush();
		fw.close();
		/********* End *********/		
	}
}



第四關


編程要求
請仔細閱讀右側代碼,根據方法內的提示,在Begin - End區域內進行代碼補充,具體任務如下:

複製src/step4/input/目錄下的input.txt文件到src/step4/output/目錄下,新文件命名爲output.txt;

複製src/step4/input/目錄下的input.jpg文件到src/step4/output/目錄下,新文件命名爲output.jpg。


測試說明
補充完代碼後,點擊測評,平臺會對你編寫的代碼進行測試,當你的結果與預期輸出一致時,即爲通過。



參考代碼

package step4;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Task {
	
	public void task() throws IOException{
		/********* Begin *********/
		String file1 = "src/step4/input/input.txt";
		FileInputStream fr = new FileInputStream(file1);
		byte[] b = new byte[1024];
		int len = 0;

		String file2 = "src/step4/output/output.txt";
		FileOutputStream fw = new FileOutputStream(file2);
		while((len = fr.read(b))!=-1){
			fw.write(b,0,len);
		}
		fr.close();
		fw.close();

		String file3 = "src/step4/input/input.jpg";
		String file4 = "src/step4/output/output.jpg";
		FileInputStream fs = new FileInputStream(file3); //定義文件輸入流讀取文件信息
		FileOutputStream fos = new FileOutputStream(file4);//定義文件輸出流寫文件
		len = 0;        //每次讀取數據的長度
		byte[] bys = new byte[1024];    //數據緩衝區
		while( (len = fs.read(bys)) != -1){
			fos.write(bys, 0, len);
		}
		//釋放資源  刷新緩衝區
		fs.close();
		fos.close();
		/********* End *********/		
	}
}

【筆記】

FileInputStream:字節文件輸入流,從文件系統中的某個文件中獲得輸入字節,用於讀取諸如圖像數據之類的原始字節流。
FileOutputStream:字節文件輸出流是用於將數據寫入到File,從程序中寫入到其他位置。
BufferedInputStream:字節緩衝輸入流,提高了讀取效率。
BufferedOutputStream:字節緩衝輸出流,提高了寫出效率。

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));
        String str;
        //一次性讀取一行
        while ((str = reader.readLine()) != null) {
            System.out.println(str);
        }

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章