zoukankan      html  css  js  c++  java
  • IO操作

     

    IO流

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

    IO流常用基类

    字节流的抽象基类
    InputStream OutputStream

    字符流的抽象基类
    Reader Writer

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

    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();
                }
    
            }
        }
    
    }
    fileRead fileWrite
    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();
                    }
                }
            }
        }
    }
    buff


    装饰设计模式,
    当想要对已有的对象进行功能增强时,
    可以自定义类,将已有的对象传入,基于已有的功能,并提供增强工功能。
    那么自定义的该类成为装饰类。

    装饰模式和继承的区别
    装饰模式比继承要灵活,避免了继承体系的臃肿。而且降低了类与类之间的关系。。
    装饰类因为增强已有对象,具备的功能和已有的相同,只不过提供了更强的功能。
    所以装饰类和被装饰类通常都是属于同一个体系中的。

    /*****************************************************************************************************/

    字符流读写

    package cn.soldier.oop;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class InputAndOutputStream {
        private static final String filePath = "OutputStream.txt";
    
        public static void main(String[] args) throws FileNotFoundException,
                IOException {
            // output();
            // input();
            // copy();
            // buffOutput();
            // buffInput();
            // buffCoyp();
    
        }
    
        private static void buffCoyp() throws FileNotFoundException, IOException {
            BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(
                            "E:\ProgramCode\Java\MyEclipse 10\oop\bin\cn\soldier\oop\个人.jpg"));
    
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream("个人.jpg"));
    
            int len = 0;
            while ((len = bis.read()) != -1) {
                bos.write(len);
            }
            System.out.println("拷贝结束");
    
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static void buffInput() throws FileNotFoundException, IOException {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                    filePath));
    
            byte[] buff = new byte[100];
            int len = 0;
            while ((len = bis.read(buff)) != -1) {
                System.out.println(new String(buff, 0, len));
            }
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        private static void buffOutput() throws FileNotFoundException, IOException {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(filePath));
            for (int i = 0; i < 1000; i++) {
                bos.write('f');
            }
            bos.flush();
            System.out.println("输出结束");
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static void copy() throws FileNotFoundException, IOException {
    
            FileInputStream fis = new FileInputStream(
                    "E:\ProgramCode\Java\MyEclipse 10\oop\bin\cn\soldier\oop\个人.jpg");
            FileOutputStream fos = new FileOutputStream("个人.jpg");
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = fis.read(buff)) != -1) {
                fos.write(buff, 0, len);
                System.out.println(len);
            }
            System.out.println("输出完成");
    
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static void input() throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream(filePath);
            // 读取到字节数组
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = fis.read(buff)) != -1) {
                System.out.println(new String(buff, 0, len));
            }
            // 逐个字节读取
            // int ch = 0;
            // while ((ch = fin.read()) != -1) {
            // System.out.println(ch);
            // }
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static void output() throws FileNotFoundException, IOException {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write("呵呵".getBytes());
            fos.flush();
            System.out.println("输出完成");
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    OutputStream InputStream

     转换流 system.in system.out

    package cn.soldier.oop;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    
    public class SystemIn {
        public static void main(String[] args) throws IOException {
    
            // method();
            // 简写
            method1();
        }
    
        private static void method() throws IOException {
            InputStream in = System.in;
            //
            InputStreamReader isr = new InputStreamReader(in);
            //
            BufferedReader bufr = new BufferedReader(isr);
            //
            OutputStream out = System.out;
            //
            OutputStreamWriter osw = new OutputStreamWriter(out);
            //
            BufferedWriter bufw = new BufferedWriter(osw);
    
            String line = null;
            while ((line = bufr.readLine()) != null) {
                if ("over".equals(line)) {
                    break;
                } else {
                    bufw.write(line);
                    bufw.flush();
                    bufw.newLine();
                }
            }
            in.close();
        }
    
        private static void method1() throws IOException {
            BufferedReader bufr = new BufferedReader(new InputStreamReader(
                    System.in));
            //
            BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(
                    System.out));
            //
            String line = null;
            while ((line = bufr.readLine()) != null) {
                if ("over".equals(line)) {
                    break;
                } else {
                    bufw.write(line);
                    bufw.newLine();
                    bufw.flush();
                }
            }
        }
    }
    System in out

    File类
    用来将文件或者文件夹封装成对象。
    方便对文件与文件夹操作
    file对象可以作为参数传递给流的构造函数。

    package cn.soldier.oop;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class FileDemo {
        public static void main(String[] args) {
            File dir = new File("G:\cj2");
            // showDir(dir, 0);
            // removeDir(dir);
    
            List<File> list = new ArrayList<File>();
            fileTolist(dir, list);
            writeToFile(list, "G:\cj2\path.txt");
        }
    
        public static String getLevel(int level) {
    
            StringBuilder sb = new StringBuilder();
            sb.append("|--");
            for (int i = 0; i < level; i++) {
                sb.insert(0, "|  ");
            }
            return sb.toString();
        }
    
        public static void showDir(File dir, int leverl) {
            System.out.println(getLevel(leverl) + dir.getName());
            leverl++;
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
    
                if (files[i].isDirectory()) {
                    showDir(files[i], leverl);
                } else {
                    System.out.println(getLevel(leverl) + files[i]);
                }
            }
        }
    
        public static void removeDir(File dir) {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    removeDir(files[i]);
                } else {
                    System.out.println(files[i].toString() + "::"
                            + files[i].delete());
                }
                System.out.println(dir + "::" + dir.delete());
            }
        }
    
        public static void fileTolist(File dir, List<File> list) {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    fileTolist(file, list);
                } else {
                    if (file.getName().endsWith(".java")) {
                        list.add(file);
                    }
                }
            }
        }
    
        public static void writeToFile(List<File> list, String filePath) {
    
            BufferedWriter bufw = null;
            try {
                bufw = new BufferedWriter(new FileWriter(filePath));
                for (File file : list) {
                    String path = file.getAbsolutePath();
                    bufw.write(path);
                    bufw.newLine();
                    bufw.flush();
                }
            } catch (IOException e) {
            } finally {
                try {
                    if (bufw != null) {
                        bufw.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    File

    Properties 配置文件

    package cn.soldier.oop;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class PropertiesDemo {
    
        public static void main(String[] args) throws IOException {
            Properties prop = new Properties();
    
            File file = new File("count.ini");
            if (!file.exists())
                file.createNewFile();
    
            FileInputStream fis = new FileInputStream(file);
            prop.load(fis);
    
            int count = 0;
            String value = prop.getProperty("times");
            if (value != null) {
                count = Integer.parseInt(value);
            }
                //
                System.out.println(count);
                count++;
                prop.setProperty("times", count+"");
                
                FileOutputStream fos = new FileOutputStream(file);
                prop.store(fos, "");
                fos.close();
                fis.close();
    
        }
    
    }
    Properties
  • 相关阅读:
    sql server 错误9003:LSN无效(日志扫描号无效),对数据库的修复.
    用C#调用C++DLL时的字符串指针参数传递问题
    sql server 2005中的Service broker小示例(未完善)
    水晶报表钻取数据,在明细层导的时候,报表会从新加载,并显示主报表
    [转]gridview获取当前行索引的方法
    验证视图状态 MAC 失败的解决办法
    SQL SERVER 2005中对存储过程进行签名(转)
    MSChart图表控件的一些使用
    Repository模式
    职能式管理和流程式管理
  • 原文地址:https://www.cnblogs.com/lhy_2011/p/4042374.html
Copyright © 2011-2022 走看看