一、概述1.可以直接操作字节信息的流对象2.根据流向可以分为字节输入流和字节输出流3.顶层抽象父类分别是InputStream 和 OutputStream4.根据交互设备的不同有不同的具体子类。往下是案例案例要求要求现在d盘文件中建一个 aaa.txt文档 或者 bbb.txt文档二、InputStreamInputStream把数据源读到程序中。是一个抽象类不能直接创建对象只能由子类创建对象最长用的子类FileInputStream用于和磁盘上的文件进行交互。FileInputStream构造方法FileInputStreamFile f将file对象封装成字节输入流将来读取文件中的信息FileInputStreamString path将文件路径字符串封装成字节输入流将来读取信息FileInputStream中的方法read();一次只能读到一个字节读取到的是数字是字母对应的码值如果需要转成字符可以强制转成char类型。当read 的值为-1时表示读到了文件的末尾。read(byte[] arr);将arr.length 个字节读取到arr数组中返回的是往数组中存了多少字节available(): 设置合适大小的数组skip跳过n个字节案例一单纯输出得取到的字节//单纯输出读取到的字节 public static void main(String[] args) { InputStream is null; try { // InputStream is new FileInputStream(d:/aaa.txt);用下面的也可以 is new FileInputStream(new File(d:/aaa.txt)); // is.read();一次只能读到一个字节 int x 0; while((x is.read())!-1) { System.out.println(x);//输出读取到的字节 } } catch (Exception e) { e.printStackTrace(); }finally { //关闭流 if(is!null){ try { is.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }案例二将取到的字节一次性放入byte[ ]数组中由于定义的数据太长 所以长出来的默认都是0但又怕定义数组短 所以请看案例三public static void main(String[] args) { InputStream is null; try { // InputStream is new FileInputStream(d:/aaa.txt);用下面的也可以 is new FileInputStream(new File(d:/aaa.txt)); // is.read();一次只能读到一个字节 byte[] b new byte[1024]; is.read(b);//一次性将数据读到一个数组中 for (byte b1 : b) { System.out.println(b1); } } catch (Exception e) { e.printStackTrace(); }finally { //关闭流 if(is!null){ try { is.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }案例三 解决方案 正确方法public static void main(String[] args) { InputStream is null; try { // InputStream is new FileInputStream(d:/aaa.txt);用下面的也可以 is new FileInputStream(new File(d:/aaa.txt)); // is.read();一次只能读到一个字节 byte[] b new byte[is.available()]; // 字节数组的长度读到多大就多大 is.read(b); for (int i 0; i b.length; i) { System.out.print((char)b[i]); } } catch (Exception e) { e.printStackTrace(); }finally { //关闭流 if(is!null){ try { is.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }三、OutputStreamOutputStream字节输出流的顶级抽象父类最常用子类FileOutputStreamFileOutputStream构造方法FileOutputStreamFile f将f描述的路径文件封装成字节输出流对象FileOutputStreamString path将path描述的文件路径封装成字节输出流对象OutputStream的常用方法writeint b一次写一个字节writebyte[] b使用数组缓冲区write“abc.getByte()也可以写一个字符串将字符串转换成数组示例代码public static void main(String[] args) { OutputStream os null; try { //os new FileOutputStream(d:/bbb.txt); 这样写的话会这节覆盖掉原文件中的数据 os new FileOutputStream(d:/bbb.txt,true);//这样写不会覆盖,p拼接的意思 os.write(100);//会把对应的字母写进去d byte[] bytes {65,66,67,68,69,70}; os.write(bytes);//会把对应的数组写进去 os.write(hello.getBytes());//会把对应的字符串写进去 } catch (Exception e) { e.printStackTrace(); }finally { //关闭流 if(os!null){ try { os.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }四、使用字节流文件拷贝public static void main(String[] args) { InputStream is null; OutputStream os null; try { //拷贝哪个文件就读入哪个文件 is new FileInputStream(d:/bbb.txt); os new FileOutputStream(d:/zyx.txt,true);//这样写不会覆盖,p拼接的意思 byte[] bytes new byte[1024]; int len0; while((lenis.read(bytes))!-1){ os.write(bytes,0,len); } } catch (Exception e) { e.printStackTrace(); }finally { //关闭流 if(is!null){ try { os.close(); } catch (IOException e) { throw new RuntimeException(e); } } if(os!null){ try { os.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }