FileOutputStream 类 和 FileInputStream类的简单介绍,附代码演示。以及一个复制媒体文件的小程序

来源:互联网 发布:数据归一化处理 编辑:程序博客网 时间:2024/06/08 04:18

一、FileOutputStream类。

构造函数:FileOutputStream fos = new FileOutputStream(String fileName);//参数为相关联的文件路径及名称。

主要方法:viod write(byte []b);//将b.length 个字节写入到目的文件中去。

                  void write(byte []b,int off,int len);//将指定数组中偏移量off开始的len个字节写入该文件中。(未演示)

                  void fiush();//刷新该流,将该流中的字节刷到目的文件中去。

                  void close();//关闭此流,并释放所有与该流相关的资源。同样关闭前先调用flush()方法。

二、FileInputStrean类

构造函数:FileInputStream fis = new FileInputStream(String fileName);//参数为相关联的文件路径及名称。

主要方法:int read();//从此流中读取下一个数据字节。

                  int read(byte[] b);//从此流中读取b.length个字符到该byte数组中。

                  int read(byte[] b,int off,int len);//将此流中的len个字节数据读入到byte数组偏移off(含off)向后位置中。(未演示)

                  int available();//返回剩余的文件字节数。

[java] view plain copy
  1. package ByteStream;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. public class ByteStreamDemo {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      * @throws IOException  
  12.      */  
  13.     public static void main(String[] args) throws IOException {  
  14. //      demo_Write();  
  15.         demo_Read();  
  16.               
  17.     }  
  18.   
  19.     public static void demo_Read() throws IOException {  
  20.         FileInputStream fis = new FileInputStream("C:\\out_Stream.txt");  
  21.           
  22. //      System.out.println(fis.available());//打印文件所占字节数。  
  23. //      byte []bfr = new byte[fis.available()];//不建议使用,当文件过大时,会照成内存不足。  
  24. //      fis.read(bfr);  
  25. //      System.out.println(new String(bfr));  
  26.           
  27.           
  28.         //推荐使用这个。  
  29.         byte[]ch = new byte[1024];  
  30.         int len = 0;  
  31.         while((len = fis.read(ch)) != -1){  
  32.             System.out.println(new String(ch,0,len));  
  33.         }  
  34.           
  35.           
  36.           
  37. //      int ch = 0;  
  38. //      while((ch = fis.read()) != -1){  
  39. //          System.out.print((char)ch);  
  40. //      }  
  41.           
  42.           
  43.           
  44. //      int ch = fis.read();  
  45. //      System.out.println((char)ch);  
  46.           
  47.         fis.close();  
  48.           
  49.           
  50.           
  51.     }  
  52.   
  53.     public static void demo_Write() throws IOException {  
  54.         //创建字节输出流对象。用于操作文件。  
  55.         FileOutputStream fos = new FileOutputStream("C:out_Stream.txt");  
  56.         //写字节数据。  
  57.         fos.write("hello!FileOuyputStream!".getBytes());  
  58.         fos.close();  
  59.     }  
  60.   
  61. }  


 

复制媒体文件的不同程序

[java] view plain copy
  1. package ByteStream;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9.   
  10. public class CopyMp3 {  
  11.     public static void main(String[] args) throws IOException {  
  12. //      copy_Show1();  
  13. //      copy_Show2();  
  14. //      copy_Show3();  
  15.         copy_Show4();  
  16. //      copy_Show5();  
  17.   
  18.     }  
  19.   
  20.     public static void copy_Show5() throws IOException {  
  21.         /** 
  22.          * 没用缓冲技术,超级慢。。。! 
  23.          */  
  24.         FileInputStream fis = new FileInputStream("f:\\王强 - 不想让你哭.mp3");  
  25.         FileOutputStream fos = new FileOutputStream("f:\\王强 - 不想让你哭(6).mp3");  
  26.           
  27.         int ch = 0;  
  28.         while ((ch = fis.read()) != -1) {  
  29.             fos.write(ch);  
  30.         }  
  31.         fis.close();  
  32.         fos.close();  
  33.           
  34.     }  
  35.   
  36.     public static void copy_Show4() throws IOException {  
  37.         //不建议使用。当文件过大时,会内存溢出!  
  38.         FileInputStream fis = new FileInputStream("f:\\王强 - 不想让你哭.mp3");  
  39.         BufferedInputStream bufis = new BufferedInputStream(fis);  
  40.         FileOutputStream fos = new FileOutputStream("f:\\王强 - 不想让你哭(5).mp3");  
  41.         BufferedOutputStream bufos = new BufferedOutputStream(fos);  
  42.         byte []b = new byte[bufis.available()];  
  43.         bufis.read(b);  
  44.         bufos.write(b);   
  45.         bufis.close();  
  46.         bufos.close();  
  47.     }  
  48.       
  49.   
  50.     public static void copy_Show3() throws IOException {  
  51.         FileInputStream fis = new FileInputStream("f:\\王强 - 不想让你哭.mp3");  
  52.         BufferedInputStream bufis = new BufferedInputStream(fis);  
  53.         FileOutputStream fos = new FileOutputStream("f:\\王强 - 不想让你哭(4).mp3");  
  54.         BufferedOutputStream bufos = new BufferedOutputStream(fos);  
  55.   
  56.         int ch = 0;  
  57.         while ((ch = bufis.read()) != -1) {  
  58.             bufos.write(ch);  
  59.         }  
  60.         bufis.close();  
  61.         bufos.close();  
  62.   
  63.     }  
  64.   
  65.     public static void copy_Show2() throws IOException {  
  66.         FileInputStream fis = new FileInputStream("f:\\王强 - 不想让你哭.mp3");  
  67.         BufferedInputStream bufis = new BufferedInputStream(fis);  
  68.         FileOutputStream fos = new FileOutputStream("f:\\王强 - 不想让你哭(3).mp3");  
  69.         BufferedOutputStream bufos = new BufferedOutputStream(fos);  
  70.         byte[] mybufb = new byte[1024];  
  71.         int len = 0;  
  72.         while ((len = bufis.read(mybufb)) != -1) {  
  73.             bufos.write(mybufb, 0, len);  
  74.             bufos.flush();  
  75.         }  
  76.         bufis.close();  
  77.         bufos.close();  
  78.   
  79.     }  
  80.   
  81.     public static void copy_Show1() throws FileNotFoundException, IOException {  
  82.         FileInputStream fis = new FileInputStream("f:\\王强 - 不想让你哭.mp3");  
  83.         FileOutputStream fos = new FileOutputStream("f:\\王强 - 不想让你哭(2).mp3");  
  84.         byte[] mybufb = new byte[1024];  
  85.         int len = 0;  
  86.         while ((len = fis.read(mybufb)) != -1) {  
  87.             fos.write(mybufb, 0, len);  
  88.         }  
  89.         fis.close();  
  90.         fos.close();  
  91.     }  
  92.   
  93. }  
0 0
原创粉丝点击