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

Java输入输出流简单案例

时间:2016-12-31 10:33:36      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:package   available   int   write   color   public   while   version   output   

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;

/**学生类包含姓名、年龄
 * 
 * @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输入输出流简单案例

标签:package   available   int   write   color   public   while   version   output   

原文地址:http://www.cnblogs.com/gx-java/p/6238777.html

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