Java流的分类

1.输入/输出流

输入流:只能向其读数据,不能写。

输出流:只能向其写数据,不能读。

所谓的输入输出都是相对应用程序而言的。

2.字节流/字符流

单位不同,字节流操作8位,字符流操作16位,2倍关系。

3.节点流/处理流

按照流的角色来分的。

节点流:从/向特定的IO设备读/写的流,也称低级流。

处理流:对于一个已经存在的流进行连接或包装,通过包装后的流来实现读写功能,也称高级流。

Java流概念模型

Java.io包里面有40多个类,看上去很凌乱负责,但实际有规律。所有类都继承自4个类,

InputStream

OutputStream

Reader

Writer

所有类的结构图如下:

其实是按字节流/字符流来分的。

上述4个类是抽象类的主要方法。

读,InputStream/Reader

有几个主要的方法:

Int read()读取单个字节/字符。

Int read(byte[] b/char[] c),读取给定数组的字节/字符。

Int read(byte[] b/char[] c, int off, int len)从字节/字符组中的off位置,读取len长度。

代码:

以FileInputStream和FileReader为例子:

public static void main(String[] args) throws Exception{

        // TODO Auto-generated method stub

        FileInputStream fis = new FileInputStream("TestFile1.java");

        byte[] bbuf = new byte[1024];

        int hasRead = 0;

        while((hasRead = fis.read(bbuf))>0){

            System.out.print(new String(bbuf,0,hasRead));

        }

        fis.close();

    }

这里byte数组的大小是1024,如果很小,或者为奇数的话,可能会出现读出来的是乱码。

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
fr = new FileReader("TestFile1.java");
char[] cbuf = new char[32];
int hasRead = 0;
while ((hasRead = fr.read(cbuf)) > 0) {
System.out.print(new String(cbuf, 0, hasRead));
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
fr.close();
}
}

写,OutputStream/Writer

主要的方法:

Void write(c)写入指定的c个字节/字符。

Void write(byte[] b/char[] c)写入字节/字符数组中的数据。

Void write(byte[]/char[] buf, int off, int len)从字节/字符数组中的off位置写入len长度到输出流。

由于字符流以字符为单位,Writer可以用字符串来代替字符数组,于是有了2个新的方法:

Void write(string s)写入字符串s到流。

Void write(string s, int off, int len)从字符串s的off位置处写入len长度到流。

以FileOutputStream和FileWriter为例子:

public static void main(String[] args) throws IOException {

        // TODO Auto-generated method stub

        FileInputStream fis = null;

        FileOutputStream fos = null;

        try {

            fis = new FileInputStream(

                    " TestFile1.java");

            fos = new FileOutputStream(

                    " NewFile.txt");

            byte[] bbuf = new byte[1024];

            int hasRead = 0;

            while ((hasRead = fis.read(bbuf)) > 0) {

                fos.write(bbuf, 0, hasRead);

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            fis.close();

            fos.close();

        }

    }

FileWriter:

public static void main(String[] args) throws IOException {

        // TODO Auto-generated method stub

        FileWriter fw = null;

        try {

            fw = new FileWriter("newFile2.txt");

            fw.write("静夜思\r\n");

            fw.write("床前明月光,\r\n");

            fw.write("疑是地上霜,\r\n");

            fw.write("举头望明月,\r\n");

            fw.write("低头思故乡。\r\n");

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            fw.close();

        }

    }

Java序列化和反序列化

序列化即将对象转换成数据(二进制或xml文本),反序列化则相反。

Java序列化/反序列化用到的类:ObjectOutputStream/ObjectInputStream,它们都是处理流对象,接受节点流FileOutputStream/FileInputStream.

序列化/反序列化要求被处理的类继承Serializable接口。

例子:

//Person 类
public class Person implements java.io.Serializable { public String Name; public int Age; public Person(String name,int age){
this.Name=name;
this.Age=age;
}
} public class TestSerialization { /**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub /**
* Serialize
*/
try {
ObjectOutputStream op = new ObjectOutputStream(
new FileOutputStream(
"Person.txt"));
Person p = new Person("Jackson", 24);
Person p2 = new Person("Anne", 23);
op.writeObject(p);
op.writeObject(p2);
System.out.println("person info: name is " + p.Name + " ;age is: "
+ p.Age);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /**
* DeSerialize
*/
try {
ObjectInputStream ip = new ObjectInputStream(new FileInputStream(
"Person.txt"));
Person p = (Person) ip.readObject();
Object o;
while ((o = ip.readObject()) != null) {
Person pp = (Person) o;
System.out.println("name: " + pp.Name + ",Age:" + pp.Age);
} } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

JAVA IO 学习的更多相关文章

  1. Java IO学习笔记:概念与原理

    Java IO学习笔记:概念与原理   一.概念   Java中对文件的操作是以流的方式进行的.流是Java内存中的一组有序数据序列.Java将数据从源(文件.内存.键盘.网络)读入到内存 中,形成了 ...

  2. Java IO学习笔记总结

    Java IO学习笔记总结 前言 前面的八篇文章详细的讲述了Java IO的操作方法,文章列表如下 基本的文件操作 字符流和字节流的操作 InputStreamReader和OutputStreamW ...

  3. Java IO学习笔记三

    Java IO学习笔记三 在整个IO包中,实际上就是分为字节流和字符流,但是除了这两个流之外,还存在了一组字节流-字符流的转换类. OutputStreamWriter:是Writer的子类,将输出的 ...

  4. Java IO学习笔记二

    Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...

  5. Java IO学习笔记一

    Java IO学习笔记一 File File是文件和目录路径名的抽象表示形式,总的来说就是java创建删除文件目录的一个类库,但是作用不仅仅于此,详细见官方文档 构造函数 File(File pare ...

  6. java IO 学习(三)

    java IO 学习(一)给了java io 进行分类,这一章学习这些类的常用方法 一.File 1.创建一个新的File的实例: /** * 创建一个新的File实例 */ File f = new ...

  7. Java IO学习笔记一:为什么带Buffer的比不带Buffer的快

    作者:Grey 原文地址:Java IO学习笔记一:为什么带Buffer的比不带Buffer的快 Java中为什么BufferedReader,BufferedWriter要比FileReader 和 ...

  8. Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer

    作者:Grey 原文地址:Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer ByteBuffer.allocate()与ByteBuffer.allocateD ...

  9. Java IO学习笔记三:MMAP与RandomAccessFile

    作者:Grey 原文地址:Java IO学习笔记三:MMAP与RandomAccessFile 关于RandomAccessFile 相较于前面提到的BufferedReader/Writer和Fil ...

  10. Java IO学习笔记四:Socket基础

    作者:Grey 原文地址:Java IO学习笔记四:Socket基础 准备两个Linux实例(安装好jdk1.8),我准备的两个实例的ip地址分别为: io1实例:192.168.205.138 io ...

随机推荐

  1. 深入理解Bindler

    Binder模型

  2. cf 732c

    /* cf732c 错过的最少次数 _________________________________________________________________________________ ...

  3. JSPatch 实现原理详解

    原文地址https://github.com/bang590/JSPatch/wiki/JSPatch-%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%E8%AF%A6%E8 ...

  4. SpringMVC——返回JSON数据&&文件上传下载

    --------------------------------------------返回JSON数据------------------------------------------------ ...

  5. ListView的属性及方法详解

    本文转载于:http://blog.csdn.net/vector_yi/article/details/23195411 近期在重新学习Android控件知识,目前进行到ListView,感觉这是一 ...

  6. Network Alignment(网络比对)模型

      两类模型: 第一类:two-steps method 先计算两个网络之间每两个结点的相似性,再从N1*N2对相似性中抽取N1对匹配(N1<=N2). 代表算法:IsoRank 第二类:obj ...

  7. How to create/restore a slave using GTID replication in MySQL 5.6

    MySQL 5.6 is GA! Now we have new things to play with and in my personal opinion the most interesting ...

  8. win版本对比

    Win+R 输入:slmgr.vbs -dlv 显示:最为详尽的激活信息,包括:激活ID.安装ID.激活截止日期slmgr.vbs -dli 显示:操作系统版本.部分产品密钥.许可证状态slmgr.v ...

  9. yii2的form表单样式怎么灵活控制呢?

    <?php $form = ActiveForm::begin(['id' => 'login-form', 'fieldConfig'=>[ 'template'=> &qu ...

  10. python使用xlrd模块读写excel

    1.行列索引均从0开始2.int数据被读成float数据,解决办法,if type(value) == float and value%1 == 0,value= int(value)模块读 #!/u ...