首页 > 代码库 > IO操作

IO操作

package cn.soldier.oop;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FileWriteAndReadDemo {    private static String filePath = "E:\\ProgramCode\\demo.txt";    public static void main(String[] args) {        // 标准的文件流输出处理        write();        // 标准的文件流读入处理        read();        // 模拟一个文件拷贝        copy();    }    /**     * 标准的文件流输出处理     * */    private static void write() {        FileWriter fileWriter = null;        try {            // 为true时追加写入字符            fileWriter = new FileWriter(filePath, true);            fileWriter.write("呵呵");            fileWriter.flush();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (fileWriter != null) {                    fileWriter.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }    /**     * 标准的文件流读入处理     */    private static void read() {        FileReader fileReader = null;        try {            fileReader = new FileReader(filePath);            char[] buff = new char[5];            try {                int num = 0;                // 读取字符数组,每次读的个数是字符数组的长度,如果有足够的字符的话。                while ((num = fileReader.read(buff)) != -1) {                    System.out.println("num=" + num + "..."                            + new String(buff, 0, num));                }                // 逐个字符读取                // int ch = 0;                // while ((ch = fileReader.read()) != -1) {                // System.out.println((char) ch);                // }            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } finally {            try {                if (fileReader != null) {                    fileReader.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }    /**     * 模拟一个文件拷贝     * */    private static void copy() {        FileWriter fileWriter = null;        FileReader fileReader = null;        try {            fileWriter = new FileWriter("FileWriteAndReadDemo.txt", true);            fileReader = new FileReader(                    "E:\\ProgramCode\\Java\\MyEclipse 10\\oop\\src\\cn\\soldier\\oop\\FileWriteAndReadDemo.java");            // 字符数组方式读取            char[] buff = new char[1024];            int len = 0;            while ((len = fileReader.read(buff)) != -1) {                fileWriter.write(buff, 0, len);                fileWriter.flush();                System.out.println(len + "");            }            // 逐个字符读取            // int ch = 0;            // while ((ch = fileReader.read()) != -1) {            // fileWriter.write(ch);            // fileWriter.flush();            // }            System.out.println("读完了");        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (fileWriter != null) {                    fileWriter.close();                }            } catch (IOException e) {                e.printStackTrace();            }            try {                if (fileReader != null) {                    fileReader.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }}

buff

package cn.soldier.oop;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class BufferedWriterAndReaderDemo {    private static String filePath = "E:\\ProgramCode\\demo.txt";    public static void main(String[] args) {        buffWrite();        buffRead();        buffCopy();    }    private static void buffCopy() {        FileWriter fileWriter = null;        BufferedWriter bufw = null;        FileReader fileReader = null;        BufferedReader bufr = null;        try {            fileWriter = new FileWriter("BufferedWriterAndReaderDemo.txt");            bufw = new BufferedWriter(fileWriter);            fileReader = new FileReader(                    "E:\\ProgramCode\\Java\\MyEclipse 10\\oop\\src\\cn\\soldier\\oop\\BufferedWriterAndReaderDemo.java");            bufr = new BufferedReader(fileReader);            String line = null;            while ((line = bufr.readLine()) != null) {                bufw.write(line);                bufw.newLine();                bufw.flush();            }            System.out.println("end the copy");        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (bufr != null) {                    bufr.close();                }            } catch (IOException e) {                e.printStackTrace();            }            // 关闭缓冲区就是关闭流            try {                if (bufw != null) {                    bufw.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }    private static void buffRead() {        FileReader fileReader = null;        BufferedReader bufferedReader = null;        try {            fileReader = new FileReader(filePath);            bufferedReader = new BufferedReader(fileReader);            String line = null;            try {                while ((line = bufferedReader.readLine()) != null) {                    System.out.println(line);                }            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } finally {            if (bufferedReader != null) {                try {                    bufferedReader.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    private static void buffWrite() {        FileWriter fileWriter = null;        BufferedWriter bufw = null;        try {            fileWriter = new FileWriter(filePath);            bufw = new BufferedWriter(fileWriter);            for (int i = 1; i <= 1024; i++) {                bufw.write(‘c‘);                if (i % 100 == 0) {                    bufw.newLine();                }            }            bufw.flush();            System.out.println("写入结束");        } catch (IOException e) {            e.printStackTrace();        } finally {            // 关闭缓冲区就是关闭流            if (bufw != null) {                try {                    bufw.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

 

IO流

1.IO用来处理设备之间的数据传输
2.Java对数据的操作通过流的方式
3.Java用于操作流的对象都在IO中
4.流操作数据分两种,字节流和字符流
5.流按流向可以分为:输入流和输出流

IO流常用基类

字节流的抽象基类
InputStream OutputStream

字符流的抽象基类
Reader Writer

注:由这四个派生出来额子类名称都是以其父类作为子类名的后缀

 

IO操作