一、
概述
1.可以直接操作字节信息的流对象
2.根据流向,可以分为字节输入流和字节输出流
3.顶层抽象父类分别是:InputStream 和 OutputStream
4.根据交互设备的不同,有不同的具体子类。
往下是案例,案例要求:
要求现在d盘文件中建一个 aaa.txt文档 或者 bbb.txt文档
二、InputStream
InputStream把数据源读到程序中。是一个抽象类,不能直接创建对象,只能由子类创建对象,最长用的子类FileInputStream,用于和磁盘上的文件进行交互。
FileInputStream构造方法:
FileInputStream(File f):将file对象封装成字节输入流,将来读取文件中的信息
FileInputStream(String 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); } } } }三、OutputStream
OutputStream字节输出流的顶级抽象父类,最常用子类FileOutputStream
FileOutputStream构造方法
FileOutputStream(File f):将f描述的路径文件封装成字节输出流对象
FileOutputStream(String path):将path描述的文件路径封装成字节输出流对象
OutputStream的常用方法:
- write(int b):一次写一个字节
- write(byte[] 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 len=0; while((len=is.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); } } } }