Java IO(五)字节流 FileInputStream 和 FileOutputStream

一、介绍

字节流 InputStream 和 OutputStream 是字节输入流和字节输出流的超类(父类)。FileInputStream 和 FileOutputStream 是字节流用于操作文件的子类。

(一)、FileInputStream

FileInputStream 是文件字节输入流,继承自 InputStream,通常,我们使用FileInputStream从某个文件中获得输入字节。

(二)、FileOutputStream

FileOutputStream 是文件字节输出流,继承自 OutputStream,通常,我们使用 FileOutputStream 将数据写入 File 或者 FileDescriptor  的输出流。

二、构造方法

(一)、FileInputStream构造方法

(二)、FileOutputStream构造方法

三、常用API

(一)、FileInputStream常用API

(二)、FileOutputStream常用API

四、实例

(一)、单个字节读写数据

public static void main(String[] args) {
File file = new File("test.txt");
write(file, 'A');
char r = read(file);
System.out.println(r);
} // 写入单个字节数据
public static void write(File file,int b) {
FileOutputStream fos = null;
try {
// 追加模式默认为 false ,即数据不续写到文件中
fos = new FileOutputStream(file);
fos.write(b);
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if (fos != null) {
fos.close();
}
}catch(Exception e) {
e.printStackTrace();
  }
  }
} // 读取单个字节数据
public static char read(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return (char)fis.read();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fis != null) {
fis.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
return 0;
}

(二)、字节数组读取数据

public static void main(String[] args) {
write();
read();
} public static void write() {
FileOutputStream fos = null;
File file = new File("test.txt");
byte[] buffer = "abcdefghijklmnopqrstuvwxyz".getBytes();
try {
// 指定追加模式为true
fos = new FileOutputStream(file, true);
// 以字节数组写入
fos.write(buffer);
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
  if (fos != null) {
  fos.close();
}
}catch (Exception e) {
e.printStackTrace();
}
  }
} //读取字节数组数据
public static void read() {
  File file = new File("test.txt");
  FileInputStream fis = null;
  byte[] buffer = new byte[10];
  int len = 0;
  try {
    fis = new FileInputStream(file);
    while((len = fis.read(buffer)) != -1) {
      fis.read(buffer, 0, len);
      System.out.println(new String(buffer));
    }
  }catch(Exception e) {
    e.printStackTrace();
  }finally {
    try {
      if (fis != null) {
        fis.close();
      }
    }catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Java IO(五)字节流 FileInputStream 和 FileOutputStream的更多相关文章

  1. Java IO: 其他字节流(上)

    作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本小节会简要概括Java IO中的PushbackInputStream,SequenceInputS ...

  2. java IO(五):字节流、字符流的选择规律

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  3. [Java IO]02_字节流

    概要 字节流有两个核心抽象类:InputStream 和 OutputStream.所有的字节流类都继承自这两个抽象类. InputStream 负责输入,OutputStream 负责输出. 字节流 ...

  4. Java IO之字节流

    Java中的输入是指从数据源等读到Java程序中,这里的数据源可以是文件,内存或网络连接,输出则是指从Java程序中写到目的地. 输入输出流可以分为以下几种类型(暂时不考虑File类) 类名 中文名 ...

  5. java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝

    接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...

  6. Java IO流-字节流

    2017-11-05 17:48:17 Java中的IO流按数据类型分类分为两种,一是字节流,二是字符流.字符流的出现是为了简化文本数据的读入和写出操作. 如果操作的文件是文本文件,那么使用字符流会大 ...

  7. Java—IO流 字节流

    IO流(输入流.输出流),又分为字节流.字符流. 流是磁盘或其它外围设备中存储的数据的源点或终点. 输入流:程序从输入流读取数据源.数据源包括外界(键盘.文件.网络…),即是将数据源读入到程序的通信通 ...

  8. java——io、字节流缓冲区拷贝文件、字节缓冲流

    使用try catch finally关闭文件流: 写入文件: import java.io.*; public class exp{ public static void main(String[] ...

  9. java IO流——字节流

    字节流主要操作byte类型数据,以byte数组为准,主要操作类有InputStream(字节输入流).OutputSteam(字节输出流)由于IputStream和OutputStream都是抽象类, ...

随机推荐

  1. python(open 文件)

    一.open 文件 1.open('file','mode')打开一个文件 file 要打开的文件名,需加路径(除非是在当前目录) mode 文件打开的模式 需要手动关闭 close 2.with o ...

  2. Java——Java是什么一门什么语言

    解释型语言 源代码不能直接翻译成机器语言,而是先翻译成中间代码,再由解释器对中间代码进行解释运行: 程序不需要编译,程序在运行时才翻译成机器语言,每执行一次都要翻译一次: 解释性语言代表:Python ...

  3. E. Height All the Same

    E. Height All the Same 题目大意: 给你一个n*m 的矩阵,让你在里面填数字代表这个位置的高度,数字的范围是[L,R],有两种操作,第一种就是给一位置的高度加2,第二种就是给两个 ...

  4. print函数的全面认识

    # 输出打印 数字 print(123) a = 100 print(a) # 输出打印 字符串 print('字符串123') print('''锄禾日当午 汗滴禾下土''') # 输出打印 列表 ...

  5. oracle常用字符函数

    字符函数: concat:(字符连接函数) --字符连接 select concat('con','cat') from dual; select 'co'||'nc'||'at' from dual ...

  6. 面向开发者的Docker实践

    show me the code and talk to me,做的出来更要说的明白 本文源码,请点击learnSpringboot 我是布尔bl,你的支持是我分享的动力! 一. 引入 有开发经验的都 ...

  7. 【HBase】通过Bulkload批量加载数据到Hbase表中

    目录 需求 步骤 一.开发MapReduce 二.打成Jar包放到linux执行 三.有两种办法将HFile文件加载到HBase表中 开发代码 Hadoop命令运行 需求 将hdfs上面的这个路径 / ...

  8. 解决MySQL 8.0数据库出现乱码的问题

    1.在MySQL 8.0的安装目录下创建一个my.ini文件(保存为utf8格式),然后写入以下内容: [mysql] # 设置mysql客户端默认编码 default-character-set=u ...

  9. WPF - 简单的UI框架

    实现了一个简单的WPF应用程序UI框架 ,分享出来.界面效果图如下: 运行效果如下: 喜欢的可以下载源码参考:https://github.com/DuelWithSelf/WPFEffects 左侧 ...

  10. [hdu3308]线段树

    题意:单点更新,区间LCIS(最长连续递增序列)查询.具备区间合并维护的性质,不用线段树用什么~ #pragma comment(linker, "/STACK:10240000,10240 ...