zoukankan      html  css  js  c++  java
  • Java字节流:FileInputStream FileOutputStream

    -----------------------------------------------------------------------------------

    FileInputStream
    类声明:public class FileInputStream extends InputStream
    位于java.io包下
    官方对其说明:
      A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment..
      (简单翻译:FileInputStream从文件系统中的某个文件中获得输入字节.)

    构造方法

      FileInputStream(File file);

        通过打开一个到实际文件的连接来创建一个FileInputStream实例,该文件通过文件系统中的File对象file指定。

      FileInputStream(String name);
        通过打开一个到实际文件的连接来创建一个FileInputStream实例,该文件通过文件系统中的路径名name指定。

      FileInputStream(FileDescriptor fdObj);
        通过使用文件描述符fdObj创建一个FileInputStream实例。

    主要方法:

      - int available(): 返回字节文件输入流中可读取的字节数
      - void close(): 关闭此文件输入流并释放与该流有关的系统资源.
      - protected void finalize(): 确保在不再引用文件输入流时调用其close()方法.
      - int read(): 从文件输入流中读取一个字节数据
      - int read(byte[] b): 从文件输入流中将最多b.length个字节数据读入到字节数组b中
      - int read(byte[] b,int off,int len): 从文件输入流中将最多len个字节的数据读入到字节数组b中
      - long skip(long n): 从文件输入流中跳过并丢弃n个字节的数据

    查看源代码
      FileInputStream这个类的作用就是把文件中的内容读取到程序中去,其中最关键的就是三个read方法,其源码都是通过调用native方法来实现的。

     1 public native int read() throws IOException;
     2 public int read(byte b[]) throws IOException {
     3         return readBytes(b, 0, b.length);
     4     }
     5 public int read(byte b[], int off, int len) throws IOException {
     6         return readBytes(b, off, len);
     7     }
     8 private native int readBytes(byte b[], int off, int len) throws IOException;
     9 public native long skip(long n) throws IOException;
    10 public native int available() throws IOException;
    View Code

    ------------------------------------------------------------------------------------------------------------------

    FileOutputStream
    类声明:public class FileOutputStream extends OutputStream
    位于java.io包下
    官方对其说明:
      A file output stream is an output stream for writing data to a File or to a FileDescriptor.
      (简单翻译:FileOutputStream文件输出流失用于将数据写入File或者FileDescriptor的输出流.)

    构造方法:
      FileOutputStream(File file);
        创建一个向指定File对象表示的文件中写入数据的文件输出流。

      FileOutputStream(File file,boolean append);
        创建一个向指定File对象表示的文件中写入数据的文件输出流。

      FileOutputStream(String name);
        创建一个向具有指定名称的文件中写入数据的输出文件流。

      FileOutputStream(String name,boolean append);
        创建一个向具有指定name的文件中写入数据的输出文件流。

      FileOutputStream(FileDescriptor fdObj);
        通过使用文件描述符fdObj创建一个FileOutputStream实例。

    主要方法:
      - void close(): 关闭此文件输出流并释放与该流有关的系统资源.
      - protected void finalize(): 清理文件的链接,确保在不再引用文件输出流时调用其close()方法.
      - void write(byte[] b): 将b.length个字节从指定的字节数组b写入此文件输出流。
      - void write(byte[] b,int off,int len): 将指定的字节数组b从偏移量off开始的len个字节写入此文件输出流。
      - void write(int b): 将指定的字节写入此文件输出流

    查看源代码:
      FileOutputStream这个类的作用就是把程序中的字节数据写入到指定的文件中,其中最关键的就是三个write方法,其源码都是通过调用native方法来实现的。

     1 private native void write(int b, boolean append) throws IOException;
     2 public void write(int b) throws IOException {
     3         write(b, append);
     4     }
     5 private native void writeBytes(byte b[], int off, int len, boolean append) throws IOException;
     6 public void write(byte b[]) throws IOException {
     7         writeBytes(b, 0, b.length, append);
     8     }
     9 public void write(byte b[], int off, int len) throws IOException {
    10         writeBytes(b, off, len, append);
    11     }
    View Code

    FileInputStream和FileOutputStream举例: 实现文件的复制.

    fileIn.txt文件中的内容:
      abcdefgheretrtrt

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 
     6 public class FileInputOutputStreamDemo {
     7     
     8     public static void main(String[] args) throws IOException {
     9         //目标文件
    10         File file = new File("fileIn.txt");
    11         //创建FileInputStream实例,该文件通过文件系统中的File对象file指定
    12         FileInputStream fis = new FileInputStream(file);
    13         
    14         byte[] fileInput = new byte[1024];
    15         int count = 0;
    16         int bytes = fis.read();
    17         //循环将文件fileText.txt中的内容读取到字节数组fileInput中
    18         while(bytes != -1){
    19             fileInput[count++] = (byte)bytes;
    20             bytes = fis.read();
    21         }
    22         
    23         //输出文件
    24         File fileOutput = new File("fileOut.txt");
    25         if(!fileOutput.exists()){
    26             fileOutput.createNewFile();
    27         }
    28         //创建文件输出流对象
    29         FileOutputStream fos = new FileOutputStream(fileOutput);
    30         //将字节数组fileInput中的内容输出到文件fileOut.txt中
    31         fos.write(fileInput);
    32     }
    33 }
  • 相关阅读:
    补点基础:Javascript中的类和闭包
    Extjs学习笔记之五——一个小细节renderTo和applyTo的区别
    Extjs学习笔记之八——继承和事件基础
    总结一篇
    再补点基础:Javascript的构造函数和constructor属性
    Extjs学习笔记之六——面版
    Extjs学习笔记之七——布局
    Extjs学习笔记之九——数据模型(上)
    引擎功率与扭矩哪个比较重要
    毕业三年,同学基本都辞职了,大部分人看完很有同感~
  • 原文地址:https://www.cnblogs.com/xinhuaxuan/p/6053299.html
Copyright © 2011-2022 走看看