p3 FileInputStream 和 FileOutputStream

打印 上一主题 下一主题

主题 877|帖子 877|积分 2631

FileInputStream 和 FileOutputStream



  • InputStream:字节输入流
    InputStream抽象类是所有类输入流的超类
    InputStream 常用的子类

    • FileInputStream: 文件输入流
    • BufferedInputStream:缓冲字节输入流
    • ObjectInputStream:对象字节输入流

一、FileInputStream




  • 构造方法摘要
    Constructor and DescriptionFileInputStream(File file)通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。FileInputStream(FileDescriptor fdObj)创建 FileInputStream通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。FileInputStream(String name)通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
  • 方法摘要
    Modifier and TypeMethod and Descriptionintavailable()返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。voidclose()关闭此文件输入流并释放与流相关联的任何系统资源。protected voidfinalize()确保当这个文件输入流的 close方法没有更多的引用时被调用。FileChannelgetChannel()返回与此文件输入流相关联的唯一的FileChannel对象。FileDescriptorgetFD()返回表示与此 FileInputStream正在使用的文件系统中实际文件的连接的 FileDescriptor对象。intread()从该输入流读取一个字节的数据。intread(byte[] b)从该输入流读取最多 b.length个字节的数据为字节数组。intread(byte[] b, int off, int len)从该输入流读取最多 len字节的数据为字节数组。longskip(long n)跳过并从输入流中丢弃 n字节的数据。
代码演示:
  1. import org.junit.jupiter.api.Test;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. /**
  5. * @author
  6. * @version 1.0
  7. */
  8. public class FileInputStream_ {
  9.     public static void main(String[] args) {
  10.     }
  11.     /**
  12.      * 演示读取文件
  13.      * read(byte b)单个字节的读取,效率较低
  14.      * 使用 read(byte[] b) 来读取
  15.      */
  16.     @Test
  17.     public void readFile01(){
  18.         String filePath = "e:\\hello.txt";
  19.         int readData = 0;
  20.         FileInputStream fileInputStream = null;
  21.         try {
  22.             //创建 FileInputStream 对象,用于读取文件
  23.             fileInputStream = new FileInputStream(filePath);
  24.             //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止
  25.             //如果返回-1.表示读取完毕
  26.             while((readData = fileInputStream.read()) != -1){
  27.                 System.out.print((char)readData);//转成char显示
  28.             }
  29.         } catch (IOException e) {
  30.             e.printStackTrace();
  31.         }finally {
  32.             try {
  33.                 fileInputStream.close();
  34.             } catch (IOException e) {
  35.                 e.printStackTrace();
  36.             }
  37.         }
  38.     }
  39.     @Test
  40.     public void readFile02(){
  41.         String filePath = "e:\\hello.txt";
  42.         byte[] buf = new byte[8];//一次读8个字节
  43.         int readLen = 0;
  44.         FileInputStream fileInputStream = null;
  45.         try {
  46.             //创建 FileInputStream 对象,用于读取文件
  47.             fileInputStream = new FileInputStream(filePath);
  48. //            读取的字节数最多等于b的长度。
  49. //            令k为实际读取的字节数; 这些字节将存储在元素b[0]至b[ k -1] ,使元素b[ k ]至b[b.length-1]不受影响。
  50. //            返回读取到缓冲区的总字节数,或者如果没有更多的数据,因为已经到达流的末尾,则是 -1 。
  51.             while((readLen = fileInputStream.read(buf)) != -1){
  52.                 System.out.print(new String(buf, 0, readLen));//显示
  53.             }
  54.         } catch (IOException e) {
  55.             e.printStackTrace();
  56.         }finally {
  57.             try {
  58.                 fileInputStream.close();
  59.             } catch (IOException e) {
  60.                 e.printStackTrace();
  61.             }
  62.         }
  63.     }
  64. }
  65. /*运行结果
  66.         hello world
  67.         hello world
  68. */
复制代码
二、FileOutputStream




  • 构造方法摘要
    Constructor and DescriptionFileOutputStream(File file)创建文件输出流以写入由指定的 File对象表示的文件。FileOutputStream(File file, boolean append)创建文件输出流以写入由指定的 File对象表示的文件。FileOutputStream(FileDescriptor fdObj)创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。FileOutputStream(String name)创建文件输出流以指定的名称写入文件。FileOutputStream(String name, boolean append)创建文件输出流以指定的名称写入文件。
  • 方法摘要
    Modifier and TypeMethod and Descriptionvoidclose()关闭此文件输出流并释放与此流相关联的任何系统资源。protected voidfinalize()清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的 close方法。FileChannelgetChannel()返回与此文件输出流相关联的唯一的FileChannel对象。FileDescriptorgetFD()返回与此流相关联的文件描述符。voidwrite(byte[] b)将 b.length个字节从指定的字节数组写入此文件输出流。voidwrite(byte[] b, int off, int len)将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。voidwrite(int b)将指定的字节写入此文件输出流。
代码演示:
  1. import org.junit.jupiter.api.Test;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. /**
  7. * @author
  8. * @version 1.0
  9. */
  10. public class FileOutputStream_ {
  11.     public static void main(String[] args) {
  12.     }
  13.     @Test
  14.     public void writeFile(){
  15.         String filePath = "e:\\a.txt";
  16.         FileOutputStream fileOutputStream = null;
  17.         try {
  18.             String str = "Hello World!";
  19.             //得到FileOutputStream对象
  20.             //1. new FileOutputStream(filePath) 创建方式,写入内容会默认覆盖原来的内容
  21.             //2. new FileOutputStream(filePath, append) 创建方式,append 为ture,写入内容会默认追加至原来的内容后,否则就覆盖
  22.             fileOutputStream = new FileOutputStream(filePath, true);
  23.             //写入一个字节
  24.             fileOutputStream.write('H');
  25.             //写入字符串,str.getBytes() 可以把 字符串 -》字符数组
  26.             fileOutputStream.write(str.getBytes());
  27.             /*
  28.             write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off的指定字节数组写入文件输出流
  29.              */
  30. //            fileOutputStream.write(str.getBytes(), 0, str.length());
  31.         } catch (IOException e) {
  32.             e.printStackTrace();
  33.         }finally {
  34.             try {
  35.                 fileOutputStream.close();
  36.             } catch (IOException e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

八卦阵

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表