字节流是最基本的流,文件的操作、网络数据的传输等等都依赖于字节流。而字符流常常用于读取文本类型的数据或字符串流的操作等等。
字节流的API
FileInputStream API
1、public int read() throws IOException
Reads a byte of data from this input stream. This method blocks if no input is yet available.
Returns: the next byte of data, or -1 if the end of the file is reached.
2、public int read(byte[] b) throws IOException
Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
3、public int read(byte[] b,int off, int len) throws IOException
Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.
Overrides:
read in class InputStream
Parameters:
b - the buffer into which the data is read.
off - the start offset in the destination array b
len - the maximum number of bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
FileOutputStream Api
1、public void write(int b)
throws IOException
Writes the specified byte to this file output stream. Implements the write method of OutputStream.
2、public void write(byte[] b) throws IOException
Writes b.length bytes from the specified byte array to this file output stream.
Parameters:
b - the data.
3、public void write(byte[] b, int off, int len) throws IOException
Writes len bytes from the specified byte array starting at offset off to this file output stream.
Overrides:
write in class OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
read和write方法如果不带参数,则每次读取或写入一个byte,每次返回0~255范围内的int值,读取到末尾则返回-1,使用参数byte[] b,则每次开辟b大小的缓存读取或写入数据,相比每次读取或写入一个字节,效率有很大的提升。
测试文本:
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
测试代码:
package com.it; import java.io.* public class TestIOStream { public static void testStream() throws Exception { InputStream fis = null; OutputStream fos = null; fis = new FileInputStream("Readme.txt"); fos = new FileOutputStream("copy.txt"); long num = 0; //读取字节计数 int bt = 0; //每次读入字节内容 while ((bt = fis.read()) != -1) { // System.out.print((char) bt); System.out.print((char) bt); fos.write(bt); num++; } System.out.println("读取的字节数为" + num); fis.close(); fos.close(); } /** * 缓冲的字节流测试 */ public static void testBufferedStream() { int buffer = 10; //缓冲大小 try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Readme.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.txt")); int bench = 0; byte bts[] = new byte[buffer]; //创建字节流缓存 while ((bis.read(bts)) != -1) { bos.write(bts); //将字节写入输出流中,实现文件的copy功能 bench++; } System.out.println("bench=" + bench); //将输入流缓冲区中的数据全部写出(千万记住) bos.flush(); bis.close(); bos.close(); } catch (FileNotFoundException e) { System.out.println("找不到指定的文件!"); e.printStackTrace(); } catch (IOException e) { System.out.println("文件读取时发生IO异常!"); e.printStackTrace(); } } /** * 字节流的选择读取测试 */ public static void testSelectStream() { OutputStream fos = null; int buffer = 15; try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Readme.txt")); fos = new FileOutputStream("copy3.txt"); byte bts[] = new byte[buffer]; //创建缓存 //这个方法有个陷阱,缓存buffer的大小最小为“偏移量+要读取字节数”,在此最小应该为10,否则抛IndexOutOfBoundsException异常 bis.read(bts, 5, 5); //从bts数组的第5个开始读取(包含第5个),共读取5个长度,缓存区后5个则为空字符 //将字节写入输出流中,实现文件的copy功能 fos.write(bts); bis.close(); fos.close(); } catch (FileNotFoundException e) { System.out.println("找不到指定的文件!"); e.printStackTrace(); } catch (IOException e) { System.out.println("文件读取时发生IO异常!"); e.printStackTrace(); } } public static void main(String[] args) throws Exception { // testStream(); // testBufferedStream(); testSelectStream(); System.out.println("done!!"); } }
copy3
未完,将继续添加修改
参考:http://lavasoft.blog.51cto.com/62575/95387
相关推荐
Java IO字符流和字节流,讲述了字符传输流和字节传输流的不同和作用
2、常用21个IO流:FileWriter、FileReader、...3、JAVA IO流经典代码示例,示例从易到难。代码功能涉及字节、字符、字符串、文本文件、图片、音频、视频。演示错误用法和经典用法。 4、代码的结构可查看README文件。
NULL 博文链接:https://xiongjiajia.iteye.com/blog/1683399
该代码源码资源是一个用于讲解Java IO流的示例代码库。它包含了常见的IO类和方法的使用...通过研究这些代码示例,读者将能够了解如何使用不同类型的IO类来进行文件读写、字符流、字节流、网络传输等各种常见的IO操作。
JavaIO实例,包括:字节流、字符流、缓冲流、转换流
javaIO操作各种文件,筛选文件,拷贝文件,上传下载,字符流和字节流都有,根据实际项目情况直接贴就可以了,根据文件类型选择不同的testclss就可以。全部使用效率最高的流
javaIO字节流读取文件方式总结共4页.pdf.zip
JavaIO字节输出流的总结共6页.pdf.zip
java IO流 例子 源程序 字节流,字符流。
字节流:按照字节读取数据(InputStream、OutputStream) 字符流:按照字符读取数据(Reader、Writer) 功能不同: 节点流:可以直接从数据源或目的地读写数据。 处理流:不直接连接到数据源或目的地,是处理流的流...
Java中IO流实例,包括:字节流、字符流、缓冲流、转换流的操作。
java IO流 1. 流的概念 2. 输入流和输出流 3. 字符流、字节流 4. 缓冲流 5. 转换流处理流
主要描述了常用字节流的用法,附上一些代码!
此文档属于本人当初学习java基础之IO流,所整理的文档。里面有字节流与字符流的比较,也有总结使用不同方式读取文档的demo。希望对你的学习有帮助,谢谢!
java io流总结,包括字符流,字节流,很详细。inputStream outputStream
Java 的 IO 流主要包括输入、输出两种 IO 流,每种输入、输出流有可分为字节流和字符流两大类: 字节流以字节为单位来处理输入、输出操作 字符流以字符为单位来处理输入、输出操作
- 字节流对象 - InputStream - OutputStream - 字符流对象 - Reader - Writer - 字节流与字符流的转换 - 新潮的 NIO - 缓冲区(Buffer) - 通道(Channel) - 示例:文件拷贝案例 - BIO 和 NIO 拷贝文件...
主要介绍了Java中IO流 字节流实例详解的相关资料,需要的朋友可以参考下
InputStream和OutputStream 1.DataInputStream,DataOutputStream 从Stream里读取基本类型的数据 BufferedReader,BufferedWriter 从Reader/Writer里读取基本类型的数据 2.BufferedInputStream,...