Java输入输出流简单案例

Posted Tom_gx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java输入输出流简单案例相关的知识,希望对你有一定的参考价值。

package com.jckb;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class Test {
    public static void main(String[] args) {
        // m();
        // inputStreamDemo();
        // outputStreamDemo();
        m8();
    }

    // 序列输入流
    static void m8() {
        InputStream is = null;
        ObjectInputStream ois = null;
        File file = null;
        try {
            String pathname = "D://file6.txt";
            file = new File(pathname);
            is = new FileInputStream(file);
            ois = new ObjectInputStream(is);
            Object obj=ois.readObject();
            if(obj instanceof Student){
                Student stu =(Student)obj;
                System.out.println(stu.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            
            e.printStackTrace();
        }
    }

    // 序列输出流
    static void m7() {
        OutputStream out = null;
        ObjectOutputStream os = null;
        try {
            out = new FileOutputStream("D://file6.txt");
            os = new ObjectOutputStream(out);
            Student stu = new Student("张三", 20);
            os.writeObject(stu);
            System.out.println("序列号成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.flush();
                os.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 带缓冲的字符输出流
    static void m6() {
        Writer w = null;
        BufferedWriter bw = null;
        try {
            w = new FileWriter("D://file5.txt");
            bw = new BufferedWriter(w);
            String str = "再见2016,你好2017!";
            bw.write(str);
            System.out.println("写入成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (bw != null) {
                    bw.flush();
                    bw.close();
                }
                if (w != null) {
                    w.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    // 带缓冲的字符输入流
    static void m5() {
        Reader in = null;
        BufferedReader br = null;
        try {
            in = new FileReader("D://file4.txt");
            br = new BufferedReader(in);
            String result = null;
            while ((result = br.readLine()) != null) {
                System.out.println(result);
            }
            br.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 字符输入流
    static void m4() {
        FileReader fr = null;
        try {
            fr = new FileReader("D://file4.txt");
            char[] cbuf = new char[1024];
            int result = -1;
            while ((result = fr.read(cbuf)) != -1) {
                String s = new String(cbuf, 0, result);
                System.out.println("result=" + result);
                System.out.println(s);
            }
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 字符输出流
    static void m3() {
        FileWriter w = null;
        try {
            w = new FileWriter(new File("D://file4.txt"));
            String str = "你好2017!";
            w.write(str);
            System.out.println("写入成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                w.flush();
                w.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 缓冲流
    static void m2() {
        InputStream is;
        BufferedInputStream bs = null;
        try {
            is = new FileInputStream("D:\\\\file3.txt");
            bs = new BufferedInputStream(is);
            byte[] b = new byte[24];

            int result;
            while ((result = bs.read(b)) != -1) {
                String chunk = new String(b, 0, result);
                System.out.println(chunk);
            }
            bs.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void m() {
        System.out.println(File.pathSeparator);
        System.out.println(File.pathSeparatorChar);
        System.out.println(File.separator);
        System.out.println(File.separatorChar);
        File file = new File("D:\\\\file2.doc");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean b = file.exists();
        System.out.println(b);
    }

    // 字符流的读入
    static void inputStreamDemo() {
        String pathname = "D:\\\\file3.txt";
        File file = new File(pathname);
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            System.out.println("字节数为:" + in.available());

            int data = -1;
            // read() while((data=in.read())!=-1){
            System.out.println((char) data);

            // 带缓冲区的字符流的读取read(byte[]b)
            byte[] b = new byte[in.available()];
            while (in.read(b) != -1) {
                for (byte item : b) {
                    System.out.println((char) item);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 字符流的写出
    static void outputStreamDemo() {
        String pathname = "D:\\\\file.txt";
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(new File(pathname), true);
            byte[] b = { \'a\', \'b\', \'c\', \'v\' };
            os.write(b);
            System.out.println("写入成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


package com.jckb;

import java.io.Serializable;

/**学生类包含姓名、年龄
 * 如果那个属性不需要序列号,要在前面加上关键字 transient
 * @author gx
 *
 */

public class Student implements Serializable{

    private static final long serialVersionUID = 1L;
    private String name;//姓名
    private int age ;//年龄
    
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    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;
    }

    @Override
    public String toString() {
        return name+"---"+age;
    }
    
}

 

 

以上是关于Java输入输出流简单案例的主要内容,如果未能解决你的问题,请参考以下文章

第40节:Java中的IO知识案例

java题 任选一个文件,使用文件输入流,将其数据读取出来,并打印到控制台!! 代码简单能够有注释

java文件读写

Java学习笔记6.1.2 字节流 - 文件字节输入流和文件字节输出流

Java学习笔记6.1.2 字节流 - 文件字节输入流和文件字节输出流

从零开始的Java开发1-6-4 Java输入输出流:File类绝对路径和相对路径字节流缓冲流字符流对象序列化