节点流——FileInputStream&FileOutputStream(使用)

1.流的分类:
  按照流向的不同分为:输入流 输出流
  按照处理数据单位的不通分为:字节流 字符流(处理的文本文件)
  按照角色的不通分为 节点流(直接作用于文件的)处理流

  1.IO的体系:

  抽象基类 节点流(文件流) 缓冲流
InputStream(字节流) FileInputStream BufferedInputStream
OutputStream(字节流) FileOutputStream BufferedOutputStream
  Reader(字符流) FileReader BufferedReader
  Writer(字符流) FileWriter BufferedWriter

使用demo

  1 //实现复制的方法,封好的方法
  2 
  3     public static void copy(String str, String desc) throws Exception {
  4 
  5         File f = new File(str);
  6 
  7         FileInputStream fis = new FileInputStream(f);
  8 
  9         File out = new File(desc);
 10 
 11         FileOutputStream fos = new FileOutputStream(out);
 12 
 13         byte[] b = new byte[1024];
 14 
 15         int len;
 16 
 17         while ((len = fis.read(b)) != -1) {
 18 
 19             fos.write(b, 0, len);
 20 
 21         }
 22 
 23     }
 24 
 25 //实现图片的复制
 26 
 27     @Test
 28 
 29     public void testImgCopy() throws Exception {
 30 
 31         File f = new File("/Users/lixiuming/Desktop/商品详情图片/detail-1.jpg");
 32 
 33         FileInputStream fis = new FileInputStream(f);
 34 
 35         File out = new File("/Users/lixiuming/Desktop/project/day_15/detail-1.jpg");
 36 
 37         FileOutputStream fos = new FileOutputStream(out);
 38 
 39         byte[] b = new byte[1024];
 40 
 41         int len;
 42 
 43         while ((len = fis.read(b)) != -1) {
 44 
 45             fos.write(b, 0, len);
 46 
 47         }
 48 
 49     }
 50 
 51 //从硬盘读取一个文件,并写入另外一个位置,相当于复制
 52 
 53     @Test
 54 
 55     public void testInputOutputStream() throws Exception {
 56 
 57         File in = new File("hello.txt");
 58 
 59         FileInputStream fis = new FileInputStream(in);
 60 
 61         File out = new File("hello2.txt");
 62 
 63         FileOutputStream fos = new FileOutputStream(out);
 64 
 65         byte[] b = new byte[1024];
 66 
 67         int len;
 68 
 69         while ((len = fis.read(b)) != -1) {
 70 
 71             fos.write(b, 0, len);
 72 
 73         }
 74 
 75     }
 76 
 77     @Test
 78 
 79     public void testFileOutputStream() throws Exception {
 80 
 81 //创建一个File对象,表明要写入的文件位置
 82 
 83 //输出的物理文件可以不存在,若不存在,在执行过程中会自动创建,若存在,则将原来的东西覆盖
 84 
 85         File file = new File("hello2.txt");
 86 
 87 //创建一个文件输出流对象,将File对象作为形参传递给FileOutputStream构造器中
 88 
 89         FileOutputStream fos = new FileOutputStream(file);
 90 
 91 //写入操作
 92 
 93         fos.write(new String("lixiuming lixiuming lixiuming ").getBytes());
 94 
 95 //关闭输出流
 96 
 97         fos.close();
 98 
 99     }
100 
101 //从硬盘存在的一个文件中,读取其内容到程序中,使用FileInputStream
102 
103 //要读取的文件一定要存在,否则空指针异常
104 
105     @Test
106 
107     public void testFileInputStream1() throws Exception {
108 
109 //创建一个File类的对象
110 
111         File file1 = new File("hello.txt");
112 
113 //创建一个FileInputStream类的对象
114 
115         FileInputStream fis = new FileInputStream(file1);
116 
117 //调用FileInputStream方法,实现file1文件的读取
118 
119         /**
120          * 
121          * read()读取文件的一个字节 ,有循环的话,依次指向下一个数据,
122          * 
123          */
124 
125 // int b  = fis.read();
126 
127 // while(b != -1){
128 
129 // System.out.print((char)b);
130 
131 // b  = fis.read();
132 
133 // }
134 
135         int len;// 每次读入到byte中的字节长度
136 
137         byte[] bb = new byte[1024];// 读取到的数据,返回个数(没有数据了就返回-1)
138 
139         while ((len = fis.read(bb)) != -1) {
140 
141 // for(int i=0;i<len;i++){
142 
143 // System.out.print((char)bb[i]);
144 
145 // }
146 
147             String str = new String(bb, 0, len);
148 
149             System.out.println(str);
150 
151         }
152 
153 //关闭相应的流
154 
155         fis.close();
156 
157     }
原文地址:https://www.cnblogs.com/lixiuming521125/p/6428420.html