乙個流可以理解為乙個資料的序列。輸入流表示從乙個源讀取資料,輸出流表示向乙個目標寫資料。
位元組流
參照物為自己的程式
什麼叫輸出?
程式 --> 檔案 輸出
什麼叫輸入?
檔案 --> 程式 輸入
位元組輸出流(output) -- 寫檔案
outputstream 是抽象類,是所有輸出流的父類
一次寫入乙個位元組,乙個位元組是8個二進位制位
public class demo catch (filenotfoundexception e) catch (ioexception e) finally
} catch (ioexception e)
} }
public static void test() throws filenotfoundexception, ioexception ;
fileoutputstream.write(b);
// 按位元組陣列的索引和長度寫入
fileoutputstream.write(b, 1, 2);
// 簡單寫法
fileoutputstream.write("hello".getbytes());
fileoutputstream.write("world".getbytes());
// 關閉資源
fileoutputstream.close();
}
public static void test() throws filenotfoundexception, ioexception
/*
* 位元組輸入流(讀檔案)
* inputstream(所有輸入流的父類)
* 注意:位元組流寫入的時候是乙個位元組乙個位元組的寫
* 讀取也是乙個位元組乙個位元組的讀
* * 讀取檔案流程
* 1.繫結資料原始檔(要讀那個檔案)
* 2.使用 read 方法讀
* 3.關閉資源
*/
public static void main(string args) throws ioexception
fileinputstream.close();
}
public static void test() throws filenotfoundexception, ioexception
// 利用字串的構造方法直接列印字串
system.out.println(i); // 2
system.out.println(new string(b)); // ab
i = fileinputstream.read(b);
system.out.println(i); // 2
system.out.println(new string(b)); // cd
i = fileinputstream.read(b);
system.out.println(i); // 1
system.out.println(new string(b)); // ed
i = fileinputstream.read(b);
system.out.println(i); // -1
system.out.println(new string(b)); // ed
fileinputstream.close();
}
public static void test() throws filenotfoundexception, ioexception
fileinputstream.close();
}
public static void test() throws filenotfoundexception, ioexception
/*
* 使用位元組的輸入輸出流進行檔案的複製
*/public class demo
*//*
* 用位元組陣列的方式讀寫 利用緩衝陣列讀寫,相對於位元組讀寫,效率較高
*/byte b = new byte[1024];
int len = 0;
while((len = fis.read(b)) != -1)
} catch (filenotfoundexception e) catch (ioexception e) finally
} catch (ioexception e) finally
} catch (ioexception e)
}} long end = system.currenttimemillis();
long speed = end - start;
system.out.println(speed);
}}
/*
* 需求:
* 將乙個資料夾複製到另乙個資料夾下
* * 分析思路
* 原始檔(資料夾 test) ---> 目標檔案(資料夾 xtest)
* 1.目標資料夾下,建立乙個資料夾出來
* test 資料夾,建立在 xtest 目錄下
* 2.遍歷源資料夾,找出檔案
* 把檔案讀寫到目標路徑下
*/public class demo
/* * 複製資料夾方法
* 引數
* src原始檔(要被複製的)
* dest目標檔案(要把源資料夾複製進去)
*/public static void copydir(file src, file dest) throws ioexception
// 關閉資源
fis.close();
fos.close();
} else
} }}
/*
* 需求:
* 將乙個資料夾下的所有 txt 檔案複製到另乙個資料夾下
* * 分析思路:
* 原始檔(資料夾 test) ---> 目標資料夾(xtest)
* 1.遍歷目標資料夾
* 找到檔案
* 是 txt 檔案,儲存到目標資料夾下
* 不是 txt 檔案,不執行操作
* 找到資料夾
* 重新呼叫此方法,在下一級資料夾下遍歷
*/public class demo03
public static void copydirtxtfile(file src, file dest) throws ioexception
// 關閉資源
fis.close();
fos.close();
} else
} }}// 使用檔案過濾器過濾掉不是 txt 的檔案
// 資料夾不需要過濾
// 過濾器
class myfilterbytxt implements filefilter
// 返回 txt 檔案
return pathname.getname().endswith("txt");
}}
JAVA 位元組流
一 文字位元組輸入流 fileinputstream 以位元組為操作單位,讀取文字中的資料 public class sd if args.length 1 多處使用,用變數儲存值 int len args.length 可變字串用於拼接路徑 stringbuffer sb new stringbu...
java通訊位元組流
我們都知道字串是由位元組組成的,而位元組是二進位制組成的在網路是一一傳送,從網路中讀取資訊時也是一樣的,也都是乙個乙個位元組的讀取,當我們從inputstream物件中讀取從網路上發來的資訊時,都是一次只能讀取到乙個位元組,然後再將這些位元組組裝成乙個string字串,當然位元組流僅限於文字資訊的交...
java基礎之位元組流 IO流
一 io流 輸入流 輸出流 位元組流 字元流 1 inputstream 2 outputstream 二 eof end 讀到 1就讀到結尾 三 輸入流基本方法 int b in.read 讀取乙個位元組無符號填充到int低八位.1是eof in.read byte buf in.read byt...