IO

Java的IO主要包含三个部分:1.流式部分――IO的主体部分;2.非流式部分――主要包含一些辅助流式部分的类,如:File类、RandomAccessFile类和FileDescriptor等类;3.文件读取部分的与安全相关的类,如:SerializablePermission类。以及与本地操作系统相关的文件系统的类,如:FileSystem类和Win32FileSystem类和WinNTFileSystem类。
       流式部分可以概括为:两个对应一个桥梁。两个对应指:1.字节流(Byte Stream)和字符流(Char Stream)的对应;2.输入和输出的对应。一个桥梁指:从字节流到字符流的桥梁。对应于输入和输出为InputStreamReader和OutputStreamWriter。
 
public class IO {

    public static void main(String[] args) {
        IO.copyFileByBufferReader();
    }

    // 复制图片
    public static void copyPic() {
        String srouce = "result/screenshot/passed.png";
        String target = "result/screenshot/passed_01.png";
        try {
            InputStream in = new FileInputStream(new File(srouce));
            OutputStream out = new FileOutputStream(new File(target));
            int len = 0;
            byte[] bytes = new byte[1024];
            long begin = System.currentTimeMillis();
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            long end = System.currentTimeMillis();
            System.out.println(end - begin);

            in.close();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 复制文件
    public static void copyFileByStreamReader() {
        String srouce = "result/screenshot/file.txt";
        String target = "result/screenshot/file1.txt";

        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(srouce)));
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(new File(target)));
            char[] chars = new char[1024];
            int len = 0;
            while ((len = reader.read(chars)) != -1) {
                write.write(chars, 0, len);
            }
            reader.close();
            write.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFileByReader() {
        String srouce = "result/screenshot/file.txt";
        String target = "result/screenshot/file1.txt";
        try {
            Reader reader = new FileReader(new File(srouce));
            Writer write = new FileWriter(new File(target));
            char[] chars = new char[1024];
            int len = 0;
            while ((len = reader.read(chars)) != -1) {
                write.write(chars, 0, len);
            }
            reader.close();
            write.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFileByBufferReader() {
        String srouce = "result/screenshot/file.txt";
        String target = "result/screenshot/file1.txt";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(srouce)));
            BufferedWriter write = new BufferedWriter(new FileWriter(new File(target)));
            String line = "";
            while ((line = reader.readLine()) != null) {
                write.write(line);
                write.write("\n");
            }

            reader.close();
            write.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 
posted @ 2017-08-10 12:55  wujuan  阅读(141)  评论(0编辑  收藏  举报