IO流字节流输入输出格式

写入文件:
1
import java.io.FileOutputStream; 2 import java.io.IOException; 3 import java.io.OutputStream; 4 5 import org.junit.Test; 6 7 public class FileOutputStream_Test { 8 @Test 9 public void fileOutputFormat() { 10 11 OutputStream fos = null; // 扩大fos的作用域 12 try { 13 fos = new FileOutputStream("a.txt", true); // 指定写入的文件,加个TRUE可以不覆盖文件中已有内容 14 // 三种写入方式: 15 // 以字节形式写入文件,等价于fos.write(97);因为计算机读写文件是通过二进制编码读写的 16 fos.write('a'); 17 18 // public void write(byte[] b) throws IOException 将 b.length 个字节从指定 byte 数组写入此文件输出流中。 19 fos.write(new String("HelloWorld").getBytes());// 常用 20 21 // public void write(byte[] b,int off, int len) throws IOException 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 22 fos.write(new String("HelloWorld").getBytes(), 2, 3);// 执行结果 llo 23 24 } catch (Exception e) { 25 System.out.println("指定路径不存在"); // 调用者使用 26 // e.printStackTrace(); //程序员调试使用 27 } finally { 28 //关闭流需要判断,如果之前指定路径就是错的,根本没有流的写入,如果这时关闭流,则会报异常,但是前面写的是异常的祖先,他爆出异常,后面的IO异常就被覆盖了。 29 if (fos != null) { 30 try { 31 fos.close(); 32 } catch (IOException e) { 33 System.out.println("流关闭错误"); 34 // e.printStackTrace(); 35 } 36 } 37 } 38 } 39 40 }

 读入文件

 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 import org.junit.Test;
 7 
 8 public class FileInputStream_Test {
 9     public static void main(String[] args) {
10         // fileInputFormat();
11     }
12 
13     @Test // 单元测试不能加static
14     public void fileInputFormat() {
15         InputStream fis = null;
16         byte[] bytes = new byte[1024];
17         int length = -1;
18         try {
19             fis = new FileInputStream("Z:/a.txt");
20             while ((length = fis.read(bytes)) != -1) {// fis.read(bytes)的返回值表示读取的数组中有效字节的个数,没有则是-1
21                 System.out.println(new String(bytes, 0, length));// 调用字符串的读取指定长度并输出
22             }
23         } catch (FileNotFoundException e) {
24             System.out.println("文件路径错误");
25         } catch (IOException e) {
26             System.out.println("流读入错误");
27         } finally {
28             if (fis != null) {// 关闭流
29                 try {
30                     fis.close();
31                 } catch (Exception e) {
32                     System.out.println("流关闭错误");
33                 }
34             }
35         }
36 
37     }
38 }

 

复制文件

 1 import java.io.FileInputStream;
 2 import java.io.FileOutputStream;
 3 import java.io.IOException;
 4 
 5 import org.junit.Test;
 6 
 7 public class FileCopy_Test {
 8     @Test
 9     public void copyFile() {
10         FileInputStream fis = null;
11         FileOutputStream fos = null;
12         try {
13             fis = new FileInputStream("a.txt");
14             fos = new FileOutputStream("aa.txt");
15             byte[] bytes = new byte[1024];
16             int length = -1;
17             while ((length = fis.read(bytes)) != -1) {
18                 fos.write(bytes, 0, length);
19             }
20         } catch (Exception e) {
21             e.printStackTrace();
22         } finally {
23             try {
24                 fos.close();
25             } catch (IOException e) {
26                 e.printStackTrace();
27             }
28             try {
29                 fis.close();
30             } catch (IOException e) {
31                 e.printStackTrace();
32             }
33         }
34 
35     }
36 
37 }

使用字节缓冲流

 1 import java.io.BufferedInputStream;
 2 import java.io.BufferedOutputStream;
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 
 9 import org.junit.Test;
10 
11 public class BufferedIO_Test {
12     @Test
13     public void bufferedFile() throws IOException {
14         InputStream fis = new FileInputStream("a.txt");
15         OutputStream fos = new FileOutputStream("aa.txt");
16         BufferedInputStream bis = new BufferedInputStream(fis);// 装饰者模式,字节缓冲流
17         BufferedOutputStream bos = new BufferedOutputStream(fos);
18         byte[] bytes = new byte[1024];
19         int length = -1;// 定义一个长度,该长度是读取数组返回的读取到的有效字节个数,没有就是-1,即最后一次数组多余存的都是-1
20         while ((length = bis.read(bytes)) != -1) {// 读入一个数组,然后把这个数组中的有效字节写入文件
21             bos.write(bytes, 0, length);
22         }
23         // 关闭流,先用的后关
24         bos.close();
25         bis.close();
26 
27     }
28 
29 }

 

posted @ 2019-04-10 17:43  我差两天十八岁  阅读(358)  评论(0编辑  收藏  举报