码迷,mamicode.com
首页 > 编程语言 > 详细

java--IO操作

时间:2016-04-13 12:51:05      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

java的IO类操作主要包括如下几类

 1、File类的使用。

 2、字节操作流:OutputStream、InputStream

 3、字符操作流:Reader、Writer

 4、对象序列化:serializable

InputStream

InputStream读取流有3个方法,分别为

第一个:abstract int read()    
Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.

一次从流中读取一个字节,并以0到255的integer类型代表该字节。(例如中文的字符,被拆分分两个字节,并以integer类型表示,变成两个数字了)

实例:

public class Test {
    public static void main(String[] args) {
        String string="测试使用";
        byte[] bs=string.getBytes();
        System.out.print(bs.length + "  ");
        for (int i = 0; i < bs.length; i++) {
            System.out.print(bs[i]+ "  ");
        }
        System.out.print(new String(bs));
    }

}

结果:8  -78  -30  -54  -44  -54  -71  -45  -61  测试使用  

第二个:int read(byte[] buffer)
Equivalent to read(buffer, 0, buffer.length).

相等于read(buffer, 0, buffer.length)

第三个:int read(byte[] buffer, int offset, int length)

Reads at most length bytes from this stream and stores them in the byte array b starting at offset.

从流中读取length长度的字节,并放入到从offset位置开始的b数组中。

其中read()方法是一次读取一个字节,效率是非常低的。所以最好是使用后面两个方法。

java--IO操作

标签:

原文地址:http://www.cnblogs.com/H-BolinBlog/p/5386476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!