java IO流之FileOutputStream 字节输出流 方法 复制图片类字节流文件实例

java IO流之字节输出流 FileOutputStream

  • 文件输出流是用于将数据写入 FileFileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许一个 FileOutputStream(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。

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

  • 在UTF-8格式文件中,英文字符 底层实际占用1个字节,中文字符 底层实际占用3个字节。

构造器

构造器 描述
FileOutputStream(File file) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
FileOutputStream(FileDescriptor fdObj) 创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件系统中的某个实际文件的现有连接。
FileOutputStream(String name) 创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。

方法

Modifier and Type Method Description
void close() 关闭此文件输出流并释放与此流有关的所有系统资源。
protected void finalize() 清理到文件的连接,并确保在不再引用此文件输出流时调用此流的 close 方法。
FileChannel getChannel() 返回与此文件输出流有关的唯一 FileChannel 对象。
FileDescriptor getFD() 返回与此流有关的文件描述符。
void write(byte[] b) 将 b.length 个字节从指定 byte 数组写入此文件输出流中。
void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
void write(int b) 将指定字节写入此文件输出流。

从类 java.io.OutputStream 继承的方法

  • flush:刷新此输出流并强制写出所有缓冲的输出字节。
    • flush 的常规协定是:如果此输出流的实现已经缓冲了以前写入的任何字节,则调用此方法指示应将这些字节立即写入它们预期的目标。

从类 java.lang.Object 继承的方法 clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

分布实现

复制图像数据之类的原始字节的流

在这里插入图片描述

  1. 创建源文件与目标文件对象

    //1.1有一个源图片文件
    File f1 = new File("IOStream/src/testfile/p1.png");
    //1.2有一个目标图片文件:
    File f2 = new File("IOStream/src/testfile/p2.png");
    
  2. 创建字节流对象

    //2.1有一个输入的管道 怼 到 源文件:
    FileInputStream fis = new FileInputStream(f1);
    //2.2有一个输出的管道 怼到  目标文件上:
    FileOutputStream fos = new FileOutputStream(f2);
    
  3. 文件操作,复制文件

    1. 逐字节复制

      int n = fis.read();
      while(n!=-1){
              
              
      	fos.write(n);
      	n = fis.read();
      }
      
    2. 缓冲数组

      byte[] b = new byte[1024*5];//利用缓冲数组:
      int len = fis.read(b);
      while(len!=-1){
              
              
      	fos.write(b,0,len);
      	len = fis.read(b);
      }
      
  4. 关闭流:(倒着关闭流,先用后关)

    fos.close();
    fis.close();
    

完整程序

try-catch-finally
public class OutSF {
    
    
    public static void main(String[] args) {
    
    
        //功能:完成图片的复制:
        //1.1有一个源图片文件
        File f1 = new File("IOStream/src/testfile/p1.png");
        //1.2有一个目标图片文件:
        File f2 = new File("IOStream/src/testfile/p2.png");
        //2创建输入输出的管道 怼 到 源文件:
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
    
    
            fis = new FileInputStream(f1);
            fos = new FileOutputStream(f2);
            //3.利用缓冲数组:
            byte[] b = new byte[1024*5];
            int len = fis.read(b);
            while(len!=-1){
    
    
                fos.write(b,0,len);
                len = fis.read(b);
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
            System.out.println("找不到文件");
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }finally{
    
    
            //4.关闭流:(倒着关闭流,先用后关)
            try {
    
    
                assert fos != null;
                fos.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            try {
    
    
                assert fis != null;
                fis.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}
try-with-resource
public class OutSFTC {
    
    
    public static void main(String[] args) {
    
    
        //功能:完成图片的复制:
        //1.1有一个源图片文件
        File f1 = new File("IOStream/src/testfile/p1.png");
        //1.2有一个目标图片文件:
        File f2 = new File("IOStream/src/testfile/p2.png");
        //2创建输入输出的管道 怼 到 源文件:
        try (
                FileInputStream fis = new FileInputStream(f1);
                FileOutputStream fos = new FileOutputStream(f2);
                ){
    
    
            //3.利用缓冲数组:
            byte[] b = new byte[1024*5];
            int len = fis.read(b);
            while(len!=-1){
    
    
                fos.write(b,0,len);
                len = fis.read(b);
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
            System.out.println("找不到文件");
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}
       e.printStackTrace();
        System.out.println("找不到文件");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}


猜你喜欢

转载自blog.csdn.net/m0_46530662/article/details/119703258