IO流知識回顧1

2022-06-28 02:36:08 字數 2065 閱讀 9215

5. io流

1)目的:講資料以檔案或資料夾的形式,永久性的儲存在計算機上。

2)目標:放在硬碟上的檔案。

3)file類:

4)使用io流的步驟

a)先定義要使用的檔案

file path=new file("d:"+file.separator+"text");//sdeparator表示斜槓

if(!path.exists())

file file=new file(path,"a1.txt");//檔案不存在,輸出流自動建立空白檔案

// file file1=new file("e:"+file.separator+"text","a1.txt");

// file file2=new file("e:"+file.separator+"text"+file.separator+"a2.txt");

//// if(!file.exists()) catch (ioexception e)

// }

b)選擇合適的流

分類:按照資料流的內容分:分為位元組流(stream,多**)和字元流(reader,writer,文字)

按照資料流的傳送方向分:分為輸入流(input,reader)和輸出流(output,wirter)

按照資料的級別,分為基礎流和包裝流

c)進行讀寫操作

d) 關閉流

//再從檔案中讀取資料

// try

// system.out.println(s);

// reader.close();

// } catch (filenotfoundexception e) catch (ioexception e)

注意:a)任何輸出流,都是預設寫覆蓋,例項化輸出流的時候,預設把該檔案原來的資料都清空

b)如果檔案不存在,輸出流會自動建立乙個空白檔案,不會報異常。 但是讀操作是,不會建立新檔案,會報檔案找不到的異常

5)緩衝流: bufferedreader,bufferedwriter  

public static void main(string args) 

file file= new file(path,"time.txt");

if(!file.exists()) catch (ioexception e)

}datetimeformatter formatter = datetimeformatter.ofpattern("yyyy年mm月dd日 eee hh:mm:ss");

localdatetime now = localdatetime.now();

string nowtime= formatter.format(now);

try

br.close();

if(str1.length()==0&&str2.length()==0)else

bufferedwriter bw = new bufferedwriter(new filewriter(file));

bw.write(num+"\n");//ascii

bw.write(nowtime);

bw.flush();//緩衝

bw.close();

} catch (filenotfoundexception e) catch (ioexception e)

}}

基礎回顧 關於IO流

一.認識file類 關於file類的方法如下 1 檔名 getname 檔名 路徑名 getpath 路徑名 getabsolutefile 絕對路徑所對應的file物件 getabsolutepath 絕對路徑名 getparent 父目錄 相對路徑的父目錄,可能為null 如.刪除本身後的結果 ...

安卓io流再回顧

之前學習io模糊模糊的,現在寫下,方便自己理解,和以後再熟悉輕鬆些。長期更新 以概念為主。在學習之前,我們需要了解位元組與字元的概念。字元流以字元 16 位二進位制 為單位進行處理,主要用於文字檔案等 unicode 資料。在這裡可以這樣簡單理解,例如漢字,漢字就是字元,涉及到漢字文字的用字元。在大...

IO流知識補充

再過幾天就要是c 的期末考,沒辦法這幾天只能靜下心來好好看看c 的一些基礎知識 以下就是自己最近複習io流時,獲得的一些新知識。首先,是在中的一些標準輸入輸出流物件 其次,是幾個函式 1.cin.clear 將流 cin中的所有狀態值都重設為有效狀態.通俗點的 流出現錯誤就恢復,不管是遇到檔案結束符...