Java I/O系统

一、I/O系统:就是Java中用于数据写入和读取的类和方法,而这些类和方法都存在于java.io包和java.nio包中。其中的 "I" 是Input, "O" 是 Output, 所以也叫输入输出系统;

二、流:所谓流,简单的说,就是文件和程序之间传输的数据,而流也有方向,它必有源端和目的端,也就是输入端和输出端;

三、流的分类:

  1. 根据处理数据的单位不同可分为字节流和字符流    

    字节流:以字节为单位操作流

      InputStream

      OutputStream

    字符流:以字符为单位操作流

      Reader

      Writer

  2. 根据流向的不同可分为输入流和输出流

    输入流:文件 ---> 程序

      InputStream

      Reader

    输出流:程序 ---> 文件

      OutputStream

      Writer

  3. 根据流的功能可以分为节点流(低级流)和处理流(高级流,也叫过滤流)

    节点流:直接与数据相连,来操作流

    处理流:要在其他已有流的基础上,进行数据传输

四、流的使用步骤:

  1. 打开一个输入/输出流对象

  2. 读取或写入数据

  3. 释放资源,关闭输入/输出流对象

 

五、文件类

  1. File 类(文件或目录的表示),提供了管理磁盘文件和目录的基本功能。

  2. 常用的方法:

    delete() 删除文件或目录,如果此路径名表示一个目录,则该目录必须为空才能删除 

    exists() 判断文件或目录是否存在 

    isFile() 判断是否为文件 

    isDirectory() 判断是否为目录 

    length() 获得文件的长度 

    mkdir() 创建此抽象路径名指定的目录 

    mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录 

六、序列化:把对象转换为字节序列的过程称为对象的序列化。序列化能够保存对象的“全景图”。

      反序列化:把字节序列恢复为对象的过程。

  注意:要实现序列化的对象,必需是实现了Serializable接口,(Serializable接口是一个标记接口);

      使用了 transient修饰的属性不会被序列化;

七、实例:

  1. 文件类的使用

 1 package com;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.Date;
 6 
 7 /**
 8  * File类测试
 9  * @author Administrator
10  *
11  */
12 public class FileTest {
13 
14     public static void main(String[] args) {
15         // 创建File对象
16         File file = new File("E:\jg\exercise_bak.txt");  // 参数为该文件或目的的地址
17 
18         // 能否读
19         System.out.println("能否读:" + file.canRead());
20 
21         // 删除
22         System.out.println("删除成功:" + file.delete());
23 
24         // 重新创建文件对象
25         file = new File("E:\jg\exercise_bak.txt");
26 
27         // 判断文件是否存在
28         System.out.println("是否存在:" + file.exists());
29 
30         // 目录或文件名称
31         System.out.println("名称:" + file.getName());
32 
33         // 是否目录、文件
34         System.out.println("是否目录:" + file.isDirectory());
35         System.out.println("是否文件:" + file.isFile());
36 
37         // 最后一次修改时间
38         System.out.println("最后一次修改时间:" + new Date(file.lastModified()));
39 
40         // 文件大小
41         System.out.println("文件大小:" + file.length());
42 
43         // 重新创建File对象
44         file = new File("E:\jg");
45 
46         System.out.println("文件目录列表:");
47         // 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录
48         String[] list = file.list();
49         for (String string : list) {
50             System.out.println(string);
51         }
52         System.out.println("*******************************");
53 
54         // 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件对象
55         File[] files = file.listFiles();
56         for (File item : files) {
57             if (item.isDirectory()) { // 当前File对象为目录,则遍历该目录下所有子目录与文件
58                 System.out.println(item.getName() + " 目录下子目录与文件:");
59                 String[] it = item.list();
60                 for (String i : it) {
61                     System.out.println(i);
62                 }
63                 System.out.println("*******************************");
64                 continue;
65             }
66 
67             System.out.println(item.getName() + "  文件");
68         }
69 
70         // 重新创建File对象
71         file = new File("E:\jg\test\demo\test.txt");
72         if (!file.exists()) { // 文件不存在
73             // 获取文件路径
74             File dir = file.getParentFile();
75             if (!dir.exists()) { // 目录不存在,则创建路径中所有不存在的目录
76                 dir.mkdirs();
77             }85         }
86 
87     }
88 }

 

 

  2. 字节流的使用

 1 package com;
 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 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 /**
12  * 字节输入输出流测试
13  * 
14  * @author Administrator
15  *
16  */
17 public class IOTest {
18 
19     public static void main(String[] args) {
20         StringBuffer buffer = new StringBuffer(); // 字符串缓冲
21         
22         /* 输入流 */
23         InputStream in = null;
24 
25         try {
26             // 1. 打开输入流
27             in = new FileInputStream("E:\jg\exercise.txt");
28             // 2. 读取
29 //            byte[] b = new byte[128];
30             byte[] b = new byte[1024 * 4];
31             int len = in.read(b); // 返回读取到的字节数,返回-1表示读取到流结尾
32             while(len != -1){
33                 buffer.append(new String(b, 0, len)); // 将读取到的字节解析为String追加到缓冲
34                 len = in.read(b);
35             }
36 //            System.out.println("读到" + len + "字节的数据");
37             System.out.println(buffer.toString());
38         } catch (FileNotFoundException e) {
39             e.printStackTrace();
40         } catch (IOException e) {
41             e.printStackTrace();
42         } finally {
43             // 3. 释放资源,关闭输入流
44             if (in != null){
45                 try {
46                     in.close();
47                 } catch (IOException e) {
48                     e.printStackTrace();
49                 }
50             }
51         }
52         
53         /* 输出流 */
54         OutputStream out = null;
55         
56         try {
57             File file = new File("D:\test\demo\test.txt");
58             if (!file.getParentFile().exists()){ // 文件路径不存在,则创建路径中所有不存在的目录
59                 file.getParentFile().mkdirs();
60             }
61             // 1. 打开输出流
62             out = new FileOutputStream(file);
63             // 2. 写
64             out.write(buffer.toString().getBytes());
65         } catch (FileNotFoundException e) {
66             e.printStackTrace();
67         } catch (IOException e) {
68             e.printStackTrace();
69         } finally {
70             // 3. 释放输出流资源
71             if (out != null){
72                 try {
73                     out.flush();
74                     out.close();
75                 } catch (IOException e) {
76                     e.printStackTrace();
77                 }
78             }
79         }
80     }
81 }

 

 

  3. 字符流的使用

 1 package com;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.FileWriter;
 6 import java.io.IOException;
 7 import java.io.Reader;
 8 import java.io.Writer;
 9 
10 /**
11  * 字符输入输出流测试
12  * 
13  * @author Administrator
14  *
15  */
16 public class IOTest2 {
17 
18     public static void main(String[] args) {
19         StringBuffer buffer = new StringBuffer();
20 
21         /* 输入流 */
22         Reader reader = null;
23 
24         try {
25             // 1. 打开流
26             reader = new FileReader("E:\jg\exercise.txt");
27             // 2. 读取
28             char[] ch = new char[128]; // 缓冲区
29             int len;
30             do {
31                 len = reader.read(ch);
32                 if (len == -1)
33                     break;
34                 buffer.append(new String(ch, 0, len));
35             } while (len != -1);
36             System.out.println(buffer.toString());
37         } catch (FileNotFoundException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         } finally {
42             // 3. 释放资源
43             if (reader != null) {
44                 try {
45                     reader.close();
46                 } catch (IOException e) {
47                     e.printStackTrace();
48                 }
49             }
50         }
51 
52         /* 输出流 */
53 
54         Writer writer = null;
55 
56         try {
57             // 1. 打开流
58             writer = new FileWriter("d:\test.txt");
59             // 2. 写入
60             writer.write(buffer.toString());
61         } catch (IOException e) {
62             e.printStackTrace();
63         } finally {
64             // 3. 释放资源
65             if (writer != null) {
66                 try {
67                     writer.flush();
68                     writer.close();
69                 } catch (IOException e) {
70                     e.printStackTrace();
71                 }
72             }
73         }
74     }
75 }

 

原文地址:https://www.cnblogs.com/renchang/p/4472389.html