java-IO系统

1.概念:IO(输入/输出)指的是计算机与外部世界,或者一个程序与计算机的其余部分的接口。

2.流(stream)的概念:当程序需要写入数据的时候,就会开启一个通向目的地设备的流。

(1).流的特性:含有流质(数据);具有方向(读或写)。

3.IO操作的步骤:建立流、操作流、关闭流

4.文件类

(1).File构造器

 public File(String pathname)
 public File(String parent,String child)
 public File(File parent,String child)
  public File(URL uri)

(2).File对象类似于一个字符串,只代表一个文件或一个目录的路径名,所以,即使指定的文件或目录不存在,这些构造器也能成功执行。

       // 创建File对象
        File file = new File("E:exercise_bak.txt");

        // 能否读
        System.out.println("能否读:" + file.canRead());

        // 删除
        System.out.println("删除成功:" + file.delete());

        // 重新创建文件对象
        file = new File("E:\jg\exercise_bak.txt");

        // 判断文件是否存在
        System.out.println("是否存在:" + file.exists());

        // 目录或文件名称
        System.out.println("名称:" + file.getName());

        // 是否目录、文件
        System.out.println("是否目录:" + file.isDirectory());
        System.out.println("是否文件:" + file.isFile());

        // 最后一次修改时间
        System.out.println("最后一次修改时间:" + new Date(file.lastModified()));

        // 文件大小
        System.out.println("文件大小:" + file.length());

        // 重新创建File对象
        file = new File("E:\jg");

        System.out.println("文件目录列表:");
        // 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录
        String[] list = file.list();
        for (String string : list) {
            System.out.println(string);
        }
        System.out.println("*******************************");

        // 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件对象
        File[] files = file.listFiles();
        for (File item : files) {
            if (item.isDirectory()) { // 当前File对象为目录,则遍历该目录下所有子目录与文件
                System.out.println(item.getName() + " 目录下子目录与文件:");
                String[] it = item.list();
                for (String i : it) {
                    System.out.println(i);
                }
                continue;
            }

            System.out.println(item.getName() + "  文件");
        }

        // 重新创建File对象
        file = new File("E:\jg\test\demo\test.txt");
        if (!file.exists()) { // 文件不存在
            // 获取文件路径
            File dir = file.getParentFile();
            if (!dir.exists()) { // 目录不存在,则创建路径中所有不存在的目录
                dir.mkdirs();
            }try {
                // 创建空文件
                System.out.println("文件是否创建成功:" + file.createNewFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

5.流的分类

(1).区分高级流与低级流:查看该类的构造器。如果类的构造器带有一个已存在的流作为参数,那么就是高级流,反之则为低级流。

(2).按流的方向分:输入流。io包中的输入流都继承自抽象类InputStream或Reader。数据源读取数据到程序中

                         输出流。io包中的输出流都继承自抽象类OutputStream或Writer。将数据从程序写入数据目的地

(3).按流的处理数据的最小单位的不同:字节流(用于处理二进制文件)。byte为最小单位,io包中的字节流都继承自抽象类InputStream或OutputStream。

                                                  字符流(用于处理文本文件)。char为最小单位,io包中的输入流都继承自抽象类Reader或Writer。

(4).按流的功能分:节点流(效率低)。

                         处理流。

(5).字节流:低级字节流-FileInputStream或FileOutputStream...

                高级字节流-BufferedInputStream或BufferedOutputStream(缓冲流);DataInputStream或DataOutputStream(数据流)...

(6).字符流:低级字符流-FileReader或FileWriter...

                 高级字符流-BufferedReader或BufferdWriter(缓冲流);InputStreamReader或OutputStreamWriter(转换流)...

package com.lovo;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * 字符输入输出流测试
 * 
 * @author 
 *
 */
public class IOTest2 {

    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer();

        /* 输入流 */
        Reader reader = null;

        try {
            // 1. 打开流
            reader = new FileReader("E:\jg\exercise.txt");
            // 2. 读取
            char[] ch = new char[128]; // 缓冲区
            int len;
            do {
                len = reader.read(ch);
                if (len == -1)
                    break;
                buffer.append(new String(ch, 0, len));
            } while (len != -1);
            System.out.println(buffer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3. 释放资源
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        /* 输出流 */

        Writer writer = null;

        try {
            // 1. 打开流
            writer = new FileWriter("d:\test.txt");
            // 2. 写入
            writer.write(buffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3. 释放资源
            if (writer != null) {
                try {
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package com.lovo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 字节输入输出流测试
 * 
 * @author 
 *
 */
public class IOTest {

    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer(); // 字符串缓冲
        
        /* 输入流 */
        InputStream in = null;

        try {
            // 1. 打开输入流
            in = new FileInputStream("E:\jg\exercise.txt");
            // 2. 读取
//            byte[] b = new byte[128];
            byte[] b = new byte[1024 * 4];
            int len = in.read(b); // 返回读取到的字节数,返回-1表示读取到流结尾
            while(len != -1){
                buffer.append(new String(b, 0, len)); // 将读取到的字节解析为String追加到缓冲
                len = in.read(b);
            }
//            System.out.println("读到" + len + "字节的数据");
            System.out.println(buffer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3. 释放资源,关闭输入流
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        /* 输出流 */
        OutputStream out = null;
        
        try {
            File file = new File("D:\test\demo\test.txt");
            if (!file.getParentFile().exists()){ // 文件路径不存在,则创建路径中所有不存在的目录
                file.getParentFile().mkdirs();
            }
            // 1. 打开输出流
            out = new FileOutputStream(file);
            // 2. 写
            out.write(buffer.toString().getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3. 释放输出流资源
            if (out != null){
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

(7).对象流:ObjectInputStream(反序列化操作)或ObjectOutputStream(序列化操作)

     补充:

           Serializable"标记接口":实现对象序列化

           transient修饰符:该属性在写入文件时,没有被写入;读取时,也读取不到。

package com.lovo.day2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class ObjectTest {

    public static void main(String[] args) {
        Student stu = new Student("张飞", 18, true, 10.0, new Date(), new Course(
                1, "语文"));
        Student stu2 = new Student("张飞2", 18, true, 10.0, new Date(), new Course(
                2, "数学"));

        // 序列化
        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(
                    new FileOutputStream("d:\student.bin"));
            out.writeObject(stu);
            out.writeObject(stu2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 反序列化
        ObjectInputStream in = null;

        try {
            in = new ObjectInputStream(new FileInputStream("d:\student.bin"));
            Student stu1 = (Student) in.readObject();
            System.out.println(stu1);

            System.out.println("*****************");

            System.out.println(stu1.getCourse());
            
            System.out.println("##########################");
            
            Student stu21 = (Student) in.readObject();
            System.out.println(stu21);
            
            System.out.println("*****************");
            
            System.out.println(stu21.getCourse());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/moshengren-java/p/4487765.html