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

java之IO流

时间:2017-06-16 22:05:44      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:exists   drive   继承   load   exception   ext   system   磁盘   返回   

知识点:

java.io包中:
类File:实现的接口:Serializable,Comparable<File>

 含义:文件和目录路径名的抽象表示形式.

    [抽象路径名 中第一个名称是目录名,抽象路径名中第一个名称之后每个名称都是表示一个目录,最后一个名称即可以表示目录也可以表示文件]

   用户界面和操作系统使用与系统相关的 路径名字符串 来命名文件和目录。此类呈现分层路径名的一个抽象的、与系统无关的视图。

   抽象路径名 有两部分组成:

    1)一个可选的与系统相关的前缀字符串,比如盘符:“/”

    2)零个或更多字符串名称的序列

  路径名字符串<抽象路径名的字符串表示>抽象路径名<File对象>之间的转换与系统有关。

    无论是路径名字符串或抽象路径名,都可以是相对路径名或绝对路径名;

    绝对路径名是完整的路径名,不需要任何其他的信息就能定位它所表示的文件。

    相反,相对路径名必须使用借助其他路径名信息进行解释;

    Eg:getParent();方法可以获得抽象路径名的父路径名:它由路径名前缀和路径名名称序列中的每个名称(最后一个除外)组成

    对于任何具有绝对路径名的File对象,如果其绝对路径名是以某个目录的绝对路径开头,那么该目录的绝对路径名是File类的祖先。

  前缀的概念:

    对于Windows平台而言:包含盘符的路径名前缀是由驱动器号和一个":"组成;

===========================================IO流中的概念==================================================

数据源:提供原始数据的原始媒介。

流按方向分为:输入流、输出流[相对于应用程序而言的]

流按处理数据的单位:字节流、字符流

流按功能不同:节点流和处理流[又称包装流]

处理流中带缓冲区的成为缓冲流

还有转换流

打印流[PrintStream]<属于字节流>:

          标准输入流:System.in返回值

          标准输出流:System.out返回值

==============================节点流(字节流[InputStream/OutputStream]/字符流[Reader/Writer])============================

抽象类:InputStream:

抽象类:OutputStream:

抽象类:Reader:

抽象类:Writer:

InputStream的子类:FileInputStream的应用:

Eg(read()一个字节地读):

 1 package cn.zzsxt.inputstream;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 /**
 9  * 步骤:找到文件目录及文件、建立管道、读写数据、关闭管道
10  *
11  */
12 
13 /**
14  *字节流:InputStream(字节输入流)/OutputStream(字节输出流)
15  * java.io.InputStream类:抽象类
16  * --java.io.FileInputStream类:继承了InputStream,从文件系统中的某个文件中获得输入字节。
17  * 常用的构造方法:
18  *     FileInputStream(File file):通过打开一个到实际文件的连接来创建一个 FileInputStream
19  *  FileInputStream(String name): 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定  
20  * 常用的方法:
21  * read()从此输入流中读取一个数据字节。 
22  * read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
23  * read(byte[] b, int off, int len)  从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
24  * close()关闭此文件输入流并释放与此流有关的所有系统资源。
25  * 
26  */
27 public class TestInputStream {
28     public static void main(String[] args) {
29         File file = new File("D:/code/TestBreak.java");
30 //        File file2 = new File("D:/code/test/test.txt");
31         FileInputStream fis = null;
32 //        FileOutputStream fos = null;
33         try {
34             fis = new FileInputStream(file);
35 //            fos = new FileOutputStream(file2);
36             int i;
37             while((i = fis.read())!=-1) {
38 //                fos.write(i);
39                 char c = (char)i;
40                 System.out.print(c);
41             }
42         } catch (FileNotFoundException e) {
43             e.printStackTrace();
44         } catch (IOException e) {
45             e.printStackTrace();
46         } finally {
47             try {
48                 fis.close();
49             } catch (IOException e) {
50                 e.printStackTrace();
51             }
52         }
53          
54     }
55 }

Eg2:读到字节数组中(read(byte[] bs)

 1 package cn.zzsxt.inputstream;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 /**
 8  *  read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
 9  *    String(byte[] bytes)通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
10  */
11 public class TestInputStream2 {
12     public static void main(String[] args) {
13         File file = new File("D:/code/test/test.txt");
14         FileInputStream fis = null;
15         try {
16             fis = new FileInputStream(file);
17             byte[] bs = new byte[1024];
18             fis.read(bs);
19             String str = new String(bs);
20             System.out.println(str);
21         } catch (FileNotFoundException e) {
22             e.printStackTrace();
23         } catch (IOException e) {
24             e.printStackTrace();
25         }
26     }
27 }

Eg3:读到字节数组中

 1 package cn.zzsxt.inputstream;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 /**
 8  * read(byte[] b, int off, int len)从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
 9  * String(byte[] bytes, int offset, int length)通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
10  * FileInputStream(File file)  通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
11  */
12 public class TestInputStream3 {
13     public static void main(String[] args) {
14         File file = new File("D:/code/test/test.txt");
15         FileInputStream fis = null;
16         try {
17             fis = new FileInputStream(file);
18             byte[] bs = new byte[1024];//存储数据
19             
20 //            int byteNum = fis.read(bs);
21 //            String str = new String(bs,0,byteNum);
22             
23             int byteNum = fis.read(bs, 0, bs.length);//从输入流中读取数据到字节数组中
24             String str = new String(bs,0,byteNum);//解码字节数组
25             System.out.println(str);
26         } catch (FileNotFoundException e) {
27             e.printStackTrace();
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31     }
32 }

----------------END

====================缓冲流<处理流>BufferedInputStream/BufferedOutputStream/BufferedReader/BufferedWriter======================

BufferedInputStream类:[继承了InputStream抽象类;]会创建一个内部缓冲区数组

BufferedOutputStream类:[继承了OuputStream抽象类;]

BufferedReader类:[继承了Reader抽象类;]从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取

BufferedWriter类:[继承了Writer抽象类;]将文本写入的字符输出流中,从而....

字节缓冲流的应用:

Eg:

 1 package cn.zzsxt.bufferedstream;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileNotFoundException;
 8 import java.io.FileOutputStream;
 9 import java.io.IOException;
10 /**
11  *按照流的功能来分:节点流和处理流
12  *节点流可以直接操作数据源:
13  *InputStream
14  *--FileInputStream
15  *OutputStream 
16  *--FileOutputStream
17  *Reader
18  *--FileReader
19  *Writer
20  *--FileWriter
21  *
22  *
23  *
24  *处理流:不能直接操作数据源,要结合节点流进行使用,主要作用是用于简化操作和提高效率。
25  *--缓冲流
26  *字节缓冲流:BufferedInputStream/BufferedOutputStream
27  *    优点:降低对磁盘IO的访问频率,提高读写效率。
28  *java.io.BufferedInputStream类
29  *常用构造方法:
30  *    BufferedInputStream(InputStream in):创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用
31  *    BufferedInputStream(InputStream in, int size)创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
32  *常用方法:
33  * read()     参见 InputStream 的 read 方法的常规协定。 
34  * read(byte[] b, int off, int len) 从此字节输入流中给定偏移量处开始将各字节读取到指定的 byte 数组中。 
35  * close() 关闭此输入流并释放与该流关联的所有系统资源。
36  *java.io.BufferdOutputStream类:该类实现缓冲的输出流
37  * 常用的构造方法:
38  * BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
39  * BufferedOutputStream(OutputStream out, int size) 
40  * 常用的方法:
41  * write(int b)将指定的字节写入此缓冲的输出流。
42  * write(byte[] b, int off, int len)  将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此缓冲的输出流。
43  * flush()刷新此缓冲的输出流。
44  * 
45  *字符缓冲流:BufferedReader/BufferedWriter
46  *--转换流:将字节流转换为字符流
47  *InputStreamReader:将字节输入流转换为字符输入流。
48  *OutputStreamWriter:将字节输出流转为字符输出流。
49  */
50 public class TestBufferedStream {
51     public static void main(String[] args) {
52         FileInputStream fis = null;
53         FileOutputStream fos = null;
54         BufferedInputStream bis = null;
55         BufferedOutputStream bos = null;
56         try {
57             fis = new FileInputStream("D:/test/aa.txt");
58             File file = new File("F:/test/aa.txt");
59             if(file.exists()) {
60                 file.createNewFile();
61             }
62             fos = new FileOutputStream(file);
63             bis = new BufferedInputStream(fis);
64             bos = new BufferedOutputStream(fos);
65             int i;
66             while((i = bis.read())!=-1) {
67                 bos.write(i);
68             }
69             System.out.println("复制完成!");
70             bos.flush();//刷新缓冲区
71         } catch (FileNotFoundException e) {
72             e.printStackTrace();
73         } catch (IOException e) {
74             e.printStackTrace();
75         } finally {
76             try {
77                 fis.close();
78                 fos.close();
79                 bis.close();
80                 bos.close();
81             } catch (IOException e) {
82                 e.printStackTrace();
83             }
84         }
85     }
86 }

字符缓冲流的应用:

常用方法:readLine();

     write(String str);/nextLine();

Eg:

 1 package cn.zzsxt.bufferedstream;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileReader;
 8 import java.io.FileWriter;
 9 import java.io.IOException;
10 /**
11  * 字符缓冲流:BufferedReader/BufferedWriter
12  * java.io.BufferedReader类:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 
13  * 构造方法:
14  *     BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流
15  *  BufferedReader(Reader in, int sz)创建一个使用指定大小输入缓冲区的缓冲字符输入流。
16  * 常用的方法:
17  *     readLine() 读取一个文本行。
18  * 
19  * java.io.BufferedWriter类:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入
20  * 构造方法:
21  *     BufferedWriter(Writer out): 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
22  *  BufferedWriter(Writer out,int size):创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
23  * 常用方法:
24  *     newLine()写入一个行分隔符。 
25  *  flush()刷新该流的缓冲。
26  *  write(String str):将写入一个字符串。
27  *  close():关闭此流,但要先刷新它。
28  */
29 public class TestBufferedStream2 {
30     public static void main(String[] args) {
31         FileReader fr = null;
32         FileWriter fw = null;
33         BufferedReader br = null;
34         BufferedWriter bw = null;
35         try {
36             File file = new File("E:/test/log.txt");
37             File fileParent = file.getParentFile();
38 System.out.println(fileParent);
39             if(!fileParent.exists()) {
40                 fileParent.mkdirs();
41 System.out.println("创建目录成功!");
42             }
43 System.out.println(file);
44             if(!file.exists()) {
45                 file.createNewFile();
46 System.out.println("创建文件成功!");
47             }
48             fw = new FileWriter(file);//创建指定File对象的字符输入流
49             fr = new FileReader("D:/test/log.txt");
50             
51             br = new BufferedReader(fr);//创建使用默认大小缓冲区的缓冲字符输入流
52             bw = new BufferedWriter(fw);
53             String str;
54             while((str = br.readLine())!=null) {//读取一个文本行,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 
55                 bw.write(str);
56                 bw.newLine();//换行
57             }
58             bw.flush();
59             System.out.println("复制完成!");
60         } catch (FileNotFoundException e) {
61             e.printStackTrace();
62         } catch (IOException e) {
63             e.printStackTrace();
64         } finally {
65             try {
66                 fr.close();
67                 fw.close();
68                 br.close();
69                 bw.close();
70             } catch (IOException e) {
71                 e.printStackTrace();
72             }
73         }
74     }
75 }

---------END

=================================转换流<处理流>InputStreamReader/OutputStreamWriter================================

作用:将字节输入流转化为字符输入流/将字节输出流转化为字符输出流

InputStreamReader类继承了Reader抽象类:它是字节流通向字符流的桥梁,它使用指定的charset读取字节并将其解码为字符

OutputStreamWriter类继承了Writer抽象类:它是字符流通向字节流的桥梁,它实验指定的charset将要写入流中的字符编码为字节

常用的方法:

Eg:   

 1 package cn.zzsxt.converterstream;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 import java.io.InputStreamReader;
10 import java.io.OutputStreamWriter;
11 /**
12  * 转换流:InputStreamReader/OutputStreamWriter
13  * InputStreamReader:将字节输入流转换为字符输入流,继承了Reader类
14  * 常用的构造函数:
15  *     InputStreamReader(InputStream in)创建一个使用默认字符集的 InputStreamReader。
16  * 
17  * OutputStreamWriter:将字节输出流转换为字符输出流,继承了Writer类
18  *     常用的构造烦方法:
19  *      OutputStreamWriter(OutputStream out)创建一个使用默认字符集的OutputStreamWriter
20  * System.in---->InputStream:标准输入流
21  * System.out---->PrintStream:标准输出流
22  */
23 public class TestConverter {
24     public static void main(String[] args) {
25 //        Scanner input = new Scanner(System.in);
26 //        String str = input.nextLine();
27         File file = new File("D:/test/test.txt");
28         /*if(!file.exists()) {
29             try {
30                 file.createNewFile();
31             } catch (IOException e) {
32                 e.printStackTrace();
33             }
34         }*/
35         InputStreamReader isr = null;
36         BufferedReader br = null;
37         OutputStreamWriter osw = null;
38         FileOutputStream fos = null;
39         BufferedWriter bw = null;
40         try {
41             fos = new FileOutputStream(file,true);//操作File对象的字节输出流
42             osw = new OutputStreamWriter(fos);////将字符编码为字节写入到流中
43             isr = new InputStreamReader(System.in);//从流中读取字节将其解码为字符
44             br = new BufferedReader(isr);//缓冲流
45             bw = new BufferedWriter(osw);
46             String str = br.readLine();
47             while(!str.equals("exit")) {
48                 System.out.println(str);
49                 bw.write(str);
50                 bw.newLine();
51                 str = br.readLine();
52             }
53             System.out.println("数据传入完成!");
54             bw.flush();
55         } catch (FileNotFoundException e) {
56             e.printStackTrace();
57         } catch (IOException e) {
58             e.printStackTrace();
59         } finally {
60             try {
61                 br.close();
62                 bw.close();
63                 fos.close();
64                 osw.close();
65             } catch (IOException e) {
66                 e.printStackTrace();
67             }
68         }
69     }
70 }

--------END

====================================(数据流)DataInputStream/DataOutputStream====================================

作用:通过数据输出流将java的基本数据类型和字符串写入到基础输出流中,然后通过数据输入流从基础数据输入流中读取Java基本数据类型和字符串

java.DataInputStream类继承InputStream抽象类:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本的java数据类型,应用程序可以使用数据输出流写入稍后                                                                      由数据输入流读取的数据。

java.DataOutputStream类继承OutputStream抽象类:数据输出流允许应用程序将java的数据以恰当的方式写入到输出流中.然后应用程序使用数据输入流将数据读入。

两者的构造方法:DataInputStream(InputStream in);:使用指定底层的InputStream创建一个DataInputStream.

        DataOutputStream(OutputStream out);:创建一个新的数据输出流,将数据写入到指定的基础输出流中

常用方法:readUTF();:从包含的输入流中读取此操作所需要的字节数,返回一个Unicode字符串

     writeUTF(String str);:以与机器无关方式使用UTF-8编码将一个字符串写入基础输出流

数据输入流(从底层输入流中读取输出流写入的数据):

 1 package cn.zzsxt.dataio;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.DataInputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.IOException;
 8 /**
 9  *处理流:
10  *    DataInputStream/DataOutputStream类:方便对java中的基本数据类型和字符串进行操作。
11  *java.io.DataInputStream类:
12  *    构造方法:DataInputStream(InputStream in) 
13  *常用的方法:readXxx():xxx代表数据类型。
14  *  readInt():读取int类型的数据
15  *  readDouble():读取double类型的数据
16  *  readBoolean():读取boolean类型的数据
17  *  ...
18  *  readUTF():读取字符串类型的数据。 
19  */
20 public class TestDataInputStream {
21     public static void main(String[] args) {
22         DataInputStream dis = null;
23         try {
24             FileInputStream fis = new FileInputStream("F:/data.txt");//字节输入流
25             BufferedInputStream bis = new BufferedInputStream(fis);//缓冲输入流(处理流)
26             dis = new DataInputStream(bis);
27             int i = dis.readInt();
28             double d = dis.readDouble();
29             String str = dis.readUTF();
30             System.out.println("i="+i);
31             System.out.println("d="+d);
32             System.out.println("str="+str);
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35         } catch (IOException e) {
36             e.printStackTrace();
37         } finally {
38             try {
39                 dis.close();
40             } catch (IOException e) {
41                 e.printStackTrace();
42             }
43         }
44     }
45 }

数据输出流:(把java的基础数据通过数据输出流写入基础输出流中)

 Eg:

 1 package cn.zzsxt.dataio;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.DataOutputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 /**
 9  *处理流:
10  *    DataInputStream/DataOutputStream类:方便对java中的基本数据类型和字符串进行操作。二进制文件
11  *java.io.DataInputStream类:
12  *    构造方法:DataInputStream(InputStream in) 
13  *常用的方法:readXxx():xxx代表数据类型。
14  *  readInt():读取int类型的数据
15  *  readDouble():读取double类型的数据
16  *  readBoolean():读取boolean类型的数据
17  *  ...
18  *  readUTF():读取字符串类型的数据。
19  *
20  */
21 public class TestDataOutputStream {
22     public static void main(String[] args) {
23         try {
24             FileOutputStream fos = new FileOutputStream("F:/data.txt");//基础输出流(File字节输出流)
25             BufferedOutputStream bos = new BufferedOutputStream(fos);//实现缓冲的输出流
26             DataOutputStream dos = new DataOutputStream(bos);//数据输出流
27             dos.writeInt(123);
28             dos.writeDouble(3.134);
29             dos.writeUTF("helo");
30             dos.flush();
31             System.out.println("写入成功!");
32             dos.close();
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35         } catch (IOException e) {
36             e.printStackTrace();
37         }
38     }
39 }

---------END

================================对象的序列化(ObjectInputStream/ObjectOutputStream)==================================

  对象序列化:

    对象序列化[Serialization]:将java对象转化为字节序列(IO字节流)

    反序列化[DeSerialization]:从字节序列中恢复java对象

  为什么序列化:

    序列化后对象就可以转化为字节序列,对象就能保存到磁盘或者网络中,不同的计算机就可以共享对象。

  对象序列化的条件:

    只有实现了Serializable接口[标识性接口]的类,才能被序列化,Serializable接口没有任何方法,实现该接口的类不需要实现额外的方法.

      如果对象的属性是对象,属性对应类也应该实现Serializable接口。

对象序列化的过程:

package cn.zzsxt.objectiostream;

import java.io.Serializable;

public class Student implements Serializable{
    private String name;
    private int age;
    private String school;
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String name, int age, String school) {
        super();
        this.name = name;
        this.age = age;
        this.school = school;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
    }
    
    
}

============================================
package cn.zzsxt.objectiostream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/**
 *对象的序列化:将对象转化为字节序列的过程。
 *对象的反序列化:将字节序列转化为java对象的过程。
 *为什么要序列化:将对象序列化后变成字节序列可以保存在磁盘或通过网络传输,以实现对象共享(字节序列可以跨平台)。
 *序列化的条件:序列化的对象必须实现Serializable 接口
 *序列化需要使用ObjectOutputStream类将对象写入到磁盘或网络
 *java.io.ObjectOutputStream类继承了OutputStream类,按照功能划分的话属于处理流。
 *    构造方法:
 *        ObjectOutputStream(OutputStream out) 
 *    常用的方法:
 *        writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。
 */
public class TestObjectOutputStream {
    public static void main(String[] args) {
        OutputStream os = null;
        ObjectOutputStream oos = null;
        try {
            os = new FileOutputStream("F:/test.obj");
            oos = new ObjectOutputStream(os);
            Student stu = new Student("张三",29,"郑州尚学堂");
            oos.writeObject(stu);
            oos.flush();//刷新
            System.out.println("写入对象成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

反序列化的过程:

 1 package cn.zzsxt.objectiostream;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.ObjectInputStream;
 8 /**
 9  *对象的序列化:将对象转化为字节序列的过程。
10  *对象的反序列化:将字节序列转化为java对象的过程。
11  *为什么要序列化:将对象序列化后变成字节序列可以保存在磁盘或通过网络传输,以实现对象共享(字节序列可以跨平台)。
12  *序列化的条件:序列化的对象必须实现Serializable接口
13  *反序列化需要使用类ObjectInputStream将字节序列转化为对象
14  *
15  *java.io.ObjectInputStream类继承了InputStream类,按照功能划分的话属于处理流。
16  *    构造方法:
17  *        ObjectInputStream(InputStream in) 
18  *    常用的方法:
19  *        readObject() 将字节序列转为对象。
20  */
21 public class TestObjectInputStream {
22     public static void main(String[] args) {
23         InputStream is = null;
24         ObjectInputStream ois = null;
25         try {
26             is = new FileInputStream("F:/test.obj");
27             ois = new ObjectInputStream(is);
28             Object obj = ois.readObject();
29             if(obj instanceof Student) {
30                 Student stu = (Student)obj;
31                 System.out.println(stu);
32             }
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35         } catch (ClassNotFoundException e) {
36             e.printStackTrace();
37         } catch (IOException e) {
38             e.printStackTrace();
39         } finally {
40             try {
41                 ois.close();
42             } catch (IOException e) {
43                 e.printStackTrace();
44             }
45         }
46     }
47 }

 ------END

=============================================Properties类================================================

此类是线程安全的,多线程可以共享单个Properties对象而无需进行外部同步

位于java.util包中:Properties类是[继承]java.util.Hashtable<Object,Object>的子类

Properties类表示一个持久的属性集;Properties可以保存在流中或者从流中加载。属性列表中的每个键及其对应的值都是字符串

Eg:

 1 package cn.zzsxt.properties;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.util.Properties;
 8 /**
 9  * java.util.Properties类:继承了Hashtable类,采用键值对的方式进行存储。
10  *     Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
11  * *.properties文件:存储方式是按照键值对的方式进行存储:key=value 常用的构造方法:
12  * Properties():创建一个无默认值的空属性列表。 常用的方法: load(InputStream
13  * inStream)从输入流中读取属性列表(键和元素对)。 getProperty(String key)用指定的键在此属性列表中搜索属性。
14  * 
15  */
16 public class TestProperties {
17     public static void main(String[] args) {
18 //        File file = new File("C:\\workspace_neon\\java_IO_lizhenxi\\jdbc.properties");//绝对路径
19         File file = new File("jdbc.properties");//相对路径(相对于当前项目的根目录)
20         try {
21             FileInputStream fis = new FileInputStream(file);
22             Properties pps = new Properties();
23             pps.load(fis);
24             int sz = pps.size();
25             System.out.println("Properties对象中键的个数:"+sz);
26             String driver = pps.getProperty("driver");
27             String url = pps.getProperty("url");
28             String username = pps.getProperty("username");
29             String password = pps.getProperty("password");
30             System.out.println("driver="+driver);
31             System.out.println("url="+url);
32             System.out.println("username="+username);
33             System.out.println("password="+password);
34         } catch (FileNotFoundException e) {
35             e.printStackTrace();
36         } catch (IOException e) {
37             e.printStackTrace();
38         }
39     }
40 }

------------END

 

java之IO流

标签:exists   drive   继承   load   exception   ext   system   磁盘   返回   

原文地址:http://www.cnblogs.com/zhenxiLi-2017/p/7020709.html

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