java I/O 输入输出流详解

概要 :本文讲述一下几个问题:
1、java 的编码问题;
2、File类的使用
3、RandomAccessFile的使用
4、I/O流
5、Java的序列化和反序列化

一、 Java的编码问题
首先看一个例子

package IO;
import java.io.IOException;

public class EncodeDemo {
    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String s = "中文12";
        byte[] bytes1 = s.getBytes();
        for (byte b : bytes1) {
            System.out.print(Integer.toHexString(b&0xff)+" ");
        }
        System.out.println();
        byte[] bytes2 = s.getBytes("gbk");
        //一个中文占两个字节,英文一个字节
        for (byte b : bytes2) {
            System.out.print(Integer.toHexString(b&0xff)+" ");
        }
        System.out.println();
        byte[] bytes3 = s.getBytes("utf-8");
        //一个中文占用三个字节,英文占用一个字节
        for (byte b : bytes3) {
            System.out.print(Integer.toHexString(b&0xff)+" ");
        }
        System.out.println();
        byte[] bytes4 = s.getBytes("utf-16be");
        //中文英文都占连个字节
        for (byte b : bytes4) {
            System.out.print(Integer.toHexString(b&0xff)+" ");
        }
    }

}

输出结果:

d6 d0 ce c4 31 32 
d6 d0 ce c4 31 32 
e4 b8 ad e6 96 87 31 32 
4e 2d 65 87 0 31 0 32 

我们可以看到,项目默认采用gbk编码,中文占用两个字节,英文占用一个字节;采用utf-8编码,中文占用三个字节,英文占用一个字节;采用utf-16be编码,中文和英文各占两个字节。另外需要注意的是当你的字节序列为某种编码方式,当把字节序列转换成字符串时,也要用相同的解码方式,否则,会出现乱码。

二、 File类的常见操作
1、创建File对象的几种操作

import java.io.*;

public class TestFile {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        //方式一
        File file1 = new File("E:\\Demo\\test.txt");
        //方式二
        //目录名+文件名的方式
        File file2 = new File("E:\\Demo","test1.txt");
        //方式三
        File directory = new File("E:\\Demo");
        File file3 = new File(directory,"test2.txt");
        if(!file3.exists()){
            file3.createNewFile();
        }
        //考虑系统移植,可以这样创建文件
        File file4 = new File("d:" + File.separator + "demo" + File.separator + "test3.txt" );

    }

}

2、file对象的常见操作
(1)、createNewFile()
当且仅当文件或者目录不存在时,创建一个新的空文件活目录。
(2)、delete()
删除该文件
(3)、listFiles()
返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
(4)、list()
返回一个字符串数组,这些字符串指定该目录中的文件和目录。
(5)、getName()
返回此目录的目录名或者文件名
(6)、isDirectory() 判断是否为目录
例题:写一个程序,输出此目录下的所有文件和目录(包括其子目录)

    public static void listDirectory(File dir) {
        if(!dir.exists()){
            throw new IllegalArgumentException("目录"+dir+"不存在");
        }
        if(!dir.isDirectory()){
            throw new IllegalArgumentException(dir+"不是目录");
        }
        File[] files = dir.listFiles();
        for(File file:files){
            if(file.isDirectory()){
                listDirectory(file);
            }else{
                System.out.println(file);
            }
        }
    } 

例、删除一个带内容的目录,从最里层开始删除

package IO;

import java.io.File;

public class DeleteFile {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        deleteDirectory(new File("E:\\Demo\\Demo"));
    }

    public static void deleteDirectory(File directory) {
        if(!directory.exists()){
            throw new IllegalArgumentException("目录"+directory+"不存在");
        }
        if(!directory.isDirectory()){
            throw new IllegalArgumentException(directory+"不是目录");
        }
        File[] file = directory.listFiles();
        for(File f:file){
            if (f.isDirectory()) {
                deleteDirectory(f);
            } else {
                f.delete();
                System.out.println(f);
            }

        }
        directory.delete();
        System.out.println(directory.getName()+"已被删除");
    }

}

运行结果

E:\Demo\Demo\raf.dat
E:\Demo\Demo\test.txt
E:\Demo\Demo\test1.txt
E:\Demo\Demo\test2.txt
Demo已被删除

三、RandomAccessFile的使用
此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针(FilePointer) ;文件指针可以通过getFilePointer 方法读取,并通过 seek 方法设置。指定r方式用作读入,rw用于读写操作。
RandomAccessFile in = new RandomAccessFile(File file, String mode)
RandomAccessFile out = new RandomAccessFile(File file, String mode)
我们要求每条随机访问的记录都具有相同的大小。如果需要将文件指针至于第3条记录处,可以这样写:

in.seeks(2*RECORD_SIZE);
Employee e = new Employee();
e.readData(in);

四、IO流
1、节点流
(1)文件操作。字符流:FileReader和FileWriter;字节流:FileInputStream和FileOutputStream
(2)内存数组操作。字符流:CharArrayReader和CharArrayWriter;字节流:ByteArrayInputStream和ByteArrayOutputStream
DataInputStream和DataOutputStream;
2、缓冲流(处理流)
缓冲输入输出流:BufferedInputStream和BufferedOutputStream对读写数据提供了缓冲能力,提升了读写性能。BufferedInputStream 增加了缓冲输入以及支持 mark 和 reset 方法的能力。readLine() :读取一个文本行。
示例:

package TestBufferStream;
import java.io.*;
public class TestBufferReader {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args){
        // TODO Auto-generated method stub
        BufferedReader br = null;
        BufferedWriter bw = null;
        FileReader fr = null;
        FileWriter fw = null;
        try{
            fw = new FileWriter("E:\\test.txt",true);//追加方式
            bw = new BufferedWriter(fw);
            String s = null;
            for(int i= 0;i<10;i++){
                s = String.valueOf(Math.random());
                bw.write(s);
                bw.newLine();//换行
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                bw.close();
                fw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try{
            fr = new FileReader("E:\\test.txt");
            br = new BufferedReader(fr);
            String s = null;
            while((s = br.readLine())!=null){
                System.out.println(s);
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

结果

0.4824830522091411
0.37675861205729033
0.0931689534761192
0.5315970579213624
0.5340560161306888
0.2898617358135518
0.7142361269436568
0.6548241834734079
0.043023562152664785
0.9117684891987015

带缓冲的输入输出流提供了一种方式,就是一次可以读取一整行,非常方便。

3、数据输入输出流(处理流)
(1)DataInputStream和DataOutputStram分别继承InputStream和OutputStream;属于处理流,套接在InputStream和OutputStream类型的节点流上。
(2)DataInputStream和DataOutputStram允许程序包以机器无关的方式从底层输入流中读取基本 Java 数据类型或者以机器无关的方式将数据写到输出流上。
示例

package IO;

import java.io.*;

public class TestDataStream {

    /**
     * @param args
     */
    //数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。 


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        ByteArrayInputStream bis = null;
        DataInputStream dis = null;
        try {
            dos.writeDouble(Math.random());
            dos.writeBoolean(true);
            bis = new ByteArrayInputStream(bos.toByteArray());
            dis = new DataInputStream(bis);
            System.out.println(dis.available());
            System.out.println(dis.readDouble());
            System.out.println(dis.readBoolean());
            System.out.println();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                dos.close();
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

}

结果
9
0.04070152349226308
true
double类型的数据占8个字节,boolean类型数据占一个字节。所以共写进去9个字节。当从内存中读取数据时,读取顺序应该与写入数据的顺序一致,即保证数据类型一致。

4、Object流
ObjectInputStream和ObjectOutputStram,支持将一个对象以某种方式写入文件,或者从文件中读入一个对象。DataOutputStram允许将基本数据类型以机器无关的方式输出,ObjectOutputStram扩展了该接口,允许将对象、包括数组或者String输出。
五、Java的序列化和反序列化
(1) 什么是java的序列化和反序列化
把对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为对象的过程称为对象的反序列化。
(2)为什么需要序列化
将数据信息存储在文件中进行保存;
在网络上传送数据的字节序列;
(3) 如何实现对象的序列化和反序列化
只有实现了Serializable和Externalizable接口的类的对象才能被序列化;实现序列化方法:java.io.ObjectOutputStream的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,把得到的字节序列输出到一个目标输出流中。java.io.ObjectInputStream的readObject(Object obj)方法可对制定的文件进行反序列化,转化为对象。
实现对象序列化的步骤:
1).创建一个对象输出流,包装其的文件流;
2)调用writeObject(Object Object)方法,实现对象的序列化,将序列化后的对象输出到目标文件;
实现对象反序列化的步骤:
1)创建对象输入流,包装输入文件流;
2)调readObject(Object Object)方法,将读到的流转化成为已知的对象,实现字节流的反序列化;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

 class student implements Serializable {
    private static final long serialVersionUID = 1L;
    String stuName;
    String stuNum;
    String stuAge;

    public String getStuName() {
        return stuName;
    }

    public student(String stuName, String stuNum, String stuAge) {
        super();
        this.stuName = stuName;
        this.stuNum = stuNum;
        this.stuAge = stuAge;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuNum() {
        return stuNum;
    }

    public void setStuNum(String stuNum) {
        this.stuNum = stuNum;
    }

    public String getStuAge() {
        return stuAge;
    }

    public void setStuAge(String stuAge) {
        this.stuAge = stuAge;
    }
}

public class stuSerializable {
    public static void main(String[] args) throws IOException,
            ClassNotFoundException {
        student stu = new student("张三", "20150000", "21");
        File file = new File("E:/test/test3.txt");
        if (file.exists()){
            file.createNewFile();
            }
        // 对象序列化
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
                file));
        oos.writeObject(stu);
        // 对象反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        student stu1 = (student) ois.readObject();
        System.out.println(stu1.getStuName());
        System.out.println(stu1.getStuNum());
        System.out.println(stu1.getStuAge());
    }
 }

输出结果

张三
20150000
21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值