面向字节的输入输出流
来源:互联网 发布:程序员病 编辑:程序博客网 时间:2024/05/12 11:13
一:面向字节的输入流
父类InputStream为抽象类,不能被实例化。面向字节的输入流都是InputStream类的子类,其类层次结构下图所示:
下表 列出了 InputStream 的主要子类及说明
下表 列出了 InputStream 的常用方法:
二:面向字节的输出流
父类OutputStream为抽象类,不能被实例化。面向字节的输入流都是OutputStream类的子类,其类层次结构下图所示:
下表 列出了 OututStream 的主要子类及说明
下表 列出了 OutputStream 的常用方法:
三:面向字节的输入输出流简单应用
1:向文件中输入数据
/** * 通过字节向file中写数据 * * @param file */public void WriterFileByByte(File file) {FileOutputStream fos = null;byte[] bytes = new byte[1024];int len = bytes.length;for (int i = 0; i < len; i++) {bytes[i] = (byte) (i + 1);}try {fos = new FileOutputStream(file);fos.write(bytes);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fos != null)fos.close();} catch (IOException e) {e.printStackTrace();}}}2:从文件中读取数据
/** * 通过字节从file中读取数据 * * @param file */public void ReaderFileByByte(File file) {FileInputStream fis = null;try {fis = new FileInputStream(file);int c;while ((c = fis.read()) != -1) {System.out.print(c);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null)fis.close();} catch (IOException e) {e.printStackTrace();}}}3.将一个文件内的内容复制到另一个文件
/** * 文件复制操作 * */public void Coppy(File file1,File file2) {FileInputStream fil = null;FileOutputStream fos = null;try {//捕获异常fil = new FileInputStream(file1);//实例化输入流fos = new FileOutputStream(file2);//实例化输出流int temp;//从file1中通过输入流调用fil.read()读取数据,返回值为-1时表示数据读完了while ((temp = fil.read()) != -1) {fos.write((char) temp);//将数据写入file2}} catch (IOException e) {e.printStackTrace();} finally {try {if (fil != null) {fil.close();//关闭输入流}if (fos != null) {fos.close();//关闭输出流}} catch (IOException e) {e.printStackTrace();}}}
4.用ByteArrayInputStream和ByteArrayOutputStream输入输出数据
public void ArrayByteText() {// 将数据写入字节数组// ByteArrayOutputStream是一个节点流,程序会在内存内自动生成一个byte[];// 用toByteArray()获得该数组,例如:byte []a=bos.toByteArray();ByteArrayOutputStream bos = new ByteArrayOutputStream();for (int i = 0; i < 100; i++) {bos.write(i);}// 从字节数组读取数据ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());int c;while ((c = bis.read()) != -1) {System.out.print(c);}try {if (bos != null)bos.close();if (bis != null)bis.close();} catch (IOException e) {e.printStackTrace();}}
0 0
- 面向字节的输入输出流
- java入门教程-8.4Java中面向字节的输入输出流
- Java流式输入输出-面向字节
- 面向字符的输入输出流
- 字节输入输出流的代码注意事项
- 带缓存的字节输入输出流
- 字节输入输出流
- 输入输出 字节,字符流
- Java字节输入输出流
- java 字节输入输出流
- 字节数组输入输出流
- 字节输入输出流
- 字节 输入输出流 byte
- 字节流/输入输出
- 字节输入输出流
- 几种不同的字节输入输出流的效率
- 【IO流】字节输入输出流
- java IO(输入输出) 字节流
- 欢迎使用CSDN-markdown编辑器
- Robotium的使用技巧
- Disable and enable Foreign Key and Check Constraints
- c#-读取xml文件
- java设计模式,建造者模式
- 面向字节的输入输出流
- 198. House Robber
- 暗黑字符串
- FileZilla Server 设置教程
- u3d+Android
- Python文件读写IO操作
- web性能权威指南 笔记 02
- linux SIGABRT信号
- Leetcode-78. Subsets