IO流中資料傳遞的位元組流和字元流

2021-10-23 06:20:59 字數 2356 閱讀 8151

第三種:與第二種基本一致,但資料用 byte陣列儲存

file file = new file("practice1.txt");

filereader fr = null;

bufferedreader br = null;

try

} catch (filenotfoundexception e) catch (ioexception e)

file file = new file("practice1.txt");

filewriter fr = null;

bufferedwriter bw = null;

try catch (filenotfoundexception e) catch (ioexception e) finally

if(fr != null)

} catch (ioexception e)

}

關鍵點:fos.write(practice1content.getbytes());

int n = -1;

while((n = fis.read()) != -1)

file fisfile = new file("a.png");

file fosfile = new file("b.png");

outfileautocreated ofac = new outfileautocreated();

ofac.fileoutputstreamcreate(fosfile);

fileinputstream fis = null;

fileoutputstream fos = null;

try

system.out.println("結束!");

} catch (filenotfoundexception e) catch (ioexception e) finally

if(fos != null)

} catch (ioexception e)

}

int n = -1;

byte buffer = new byte[1024 * 8];

while((n = fis.read(buffer)) != -1)

// 建立乙個輸出路徑檔案的物件

file file = new file("g:\\practice1\\practice1.txt");

// 呼叫 專屬的方法為 fileoutputstream的輸出檔案路徑主動創造 目錄和文件

fileoutputstreamcreate(file);

fileoutputstream fos = null;

try // 用完請務必記得在 finally 進行關流操作

catch (filenotfoundexception e) catch (ioexception e) finally catch (ioexception e)

}}

// 輸入流物件

fileinputstream fis = null;

// 輸出流物件

fileoutputstream fos = null;

try

} catch (filenotfoundexception e) catch (ioexception e) finally

if(fos != null)

} catch (ioexception e)

}

public static void fileoutputstreamcreate(file file)

// 獲取 文件路徑內容

string documentpath = absolutepath.substring(separatorindex + 1);

// system.out.println(documentpath);

// 將 路徑用 文件檔案物件 建立

documentfile = new file(documentpath);

if(!documentfile.exists()) catch (ioexception e) }}

// 若路徑中未被找到 "." ,則直接建立 目錄

else}}

system.out.println("路徑已建立:"+ absolutepath);

} catch (exception e)

}

I O流 字元流和位元組流

一 位元組流 1 位元組輸出流 outputstream 往指定檔案寫資料 常用方法 close 釋放資源 flush 重新整理流,並強制寫出所有的緩衝的輸出位元組 write byte b 將指定的 byte 陣列寫入到輸出流 write byte b,int off,int len 將指定byt...

io位元組流和字元流

2014 11 30 晚上 位元組流system.in是基本的 inputstream 流,system.out 是基本的 outputstream 流,如果要實現字元流從控制台讀入 bufferedreader bufferedreader new bufferreader reader inpu...

IO 字元流 位元組流

io流 流按運算元據分為兩種 位元組流與字元流 流按流向分為 輸入流 輸出流 位元組流的抽象基類 inputstream outputstream 字元流的抽象基類 reader witer 這四個類派生出來的子類名稱都是以其父類名作為子類名的字尾。字元流 寫入流writer 用於操作檔案的writ...