`
SwordShadow
  • 浏览: 268263 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java IO 之字节流

    博客分类:
  • java
阅读更多

字节流是最基本的流,文件的操作、网络数据的传输等等都依赖于字节流。而字符流常常用于读取文本类型的数据或字符串流的操作等等。

 

字节流的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

 

 

 

 

 

 

 

  • 大小: 6.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics