java_IO流之FileInputStream和FileOutputStream

本文通过具体实例介绍了Java中FileInputStream和FileOutputStream的基本用法,包括文件复制、不同方式的读取及写入操作。

看,API说的多简单啊:

FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用FileReader

FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用FileWriter。 


FileInputStream与FileOutputStream都是字节流,所以它们都是用来读byte与写byte的。

上例子:

  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5.   
  6. public class FileStreamTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         // writeTest();  
  10.         // readTest_1();  
  11.         // readTest_2();  
  12.         // readTest_3();  
  13.         // copyFile();  
  14.     }  
  15.   
  16.     // 用字节流文件的读写。  
  17.     private static void copyFile() {  
  18.         FileInputStream fis = null;  
  19.         FileOutputStream fos = null;  
  20.         try {  
  21.             fis = new FileInputStream("F:\\library.rar");  
  22.             fos = new FileOutputStream("F:\\library_copy.rar");  
  23.             byte[] buf = new byte[1024];  
  24.             int len = 0;  
  25.             while ((len = fis.read(buf)) != -1) {  
  26.                 fos.write(buf, 0, len);  
  27.             }  
  28.             System.out.println("复制完成");  
  29.         } catch (FileNotFoundException e) {  
  30.             e.printStackTrace();  
  31.         } catch (IOException e) {  
  32.             e.printStackTrace();  
  33.         } finally {  
  34.             try {  
  35.                 if (fis != null) {  
  36.                     fis.close();  
  37.                 }  
  38.             } catch (IOException e) {  
  39.                 e.printStackTrace();  
  40.             }  
  41.             try {  
  42.                 if (fos != null) {  
  43.                     fos.close();  
  44.                 }  
  45.             } catch (IOException e) {  
  46.                 e.printStackTrace();  
  47.             }  
  48.         }  
  49.     }  
  50.   
  51.     private static void readTest_3() {  
  52.         FileInputStream fis = null;  
  53.         try {  
  54.             fis = new FileInputStream("D:\\log.txt");  
  55.             // available()方法是用于创建一个大小刚刚适合的缓冲区,不过如果资源文件过大的话,很容易造成内在溢出的问题。  
  56.             byte[] buf = new byte[fis.available()];  
  57.             fis.read(buf);  
  58.             System.out.println(new String(buf));  
  59.         } catch (FileNotFoundException e) {  
  60.             e.printStackTrace();  
  61.         } catch (IOException e) {  
  62.             e.printStackTrace();  
  63.         } finally {  
  64.             try {  
  65.                 if (fis != null) {  
  66.                     fis.close();  
  67.                 }  
  68.             } catch (IOException e) {  
  69.                 e.printStackTrace();  
  70.             }  
  71.         }  
  72.     }  
  73.   
  74.     private static void readTest_2() {  
  75.         FileInputStream fis = null;  
  76.         try {  
  77.             fis = new FileInputStream("D:\\log.txt");  
  78.             byte[] buf = new byte[1024];  
  79.             int len = 0;  
  80.             while ((len = fis.read(buf)) != -1) {  
  81.                 System.out.println(new String(buf, 0, len));  
  82.             }  
  83.         } catch (FileNotFoundException e) {  
  84.             e.printStackTrace();  
  85.         } catch (IOException e) {  
  86.             e.printStackTrace();  
  87.         } finally {  
  88.             try {  
  89.                 if (fis != null) {  
  90.                     fis.close();  
  91.                 }  
  92.             } catch (IOException e) {  
  93.                 e.printStackTrace();  
  94.             }  
  95.         }  
  96.     }  
  97.   
  98.     private static void readTest_1() {  
  99.         FileInputStream fis = null;  
  100.         try {  
  101.             fis = new FileInputStream("D:\\log.txt");  
  102.             int b = 0;  
  103.             while ((b = fis.read()) != -1) {  
  104.                 System.out.println((char) b);  
  105.             }  
  106.         } catch (FileNotFoundException e) {  
  107.             e.printStackTrace();  
  108.         } catch (IOException e) {  
  109.             e.printStackTrace();  
  110.         } finally {  
  111.             try {  
  112.                 if (fis != null) {  
  113.                     fis.close();  
  114.                 }  
  115.             } catch (IOException e) {  
  116.                 e.printStackTrace();  
  117.             }  
  118.         }  
  119.     }  
  120.   
  121.     private static void writeTest() {  
  122.         FileOutputStream fos = null;  
  123.         try {  
  124.             fos = new FileOutputStream("D:\\log.txt");  
  125.             // 字节流操作的是字节数组。字符流操作的是字符数组。  
  126.             fos.write("abcdefg".getBytes());  
  127.         } catch (FileNotFoundException e) {  
  128.             e.printStackTrace();  
  129.         } catch (IOException e) {  
  130.             e.printStackTrace();  
  131.         } finally {  
  132.             try {  
  133.                 if (fos != null) {  
  134.                     fos.close();  
  135.                 }  
  136.             } catch (IOException e) {  
  137.                 e.printStackTrace();  
  138.             }  
  139.         }  
  140.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值