zoukankan      html  css  js  c++  java
  • 字节流和字符流

    package com.test;
    
    import org.junit.Test;
    
    import java.io.*;
    
    public class TestIO {
        @Test
        public void test() {
            InputStream in = null;
            try {
                in = new FileInputStream("/Users/lina/Desktop/work/path.py");
                byte[] buf = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = in.read(buf)) != -1) {
                    for (int i = 0; i < bytesRead; ++i) {
                        System.out.print((char) buf[i]);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test2() {
            File file = null;
            InputStream in = null;
            try {
                file = new File("/Users/lina/Desktop/work/path.py");
                in = new FileInputStream(file);
                byte[] buf = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = in.read(buf)) != -1) {
                    for (int i = 0; i < bytesRead; ++i) {
                        System.out.print((char) buf[i]);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test3() {
            InputStream in = null;
            try {
                in = new BufferedInputStream(new FileInputStream("/Users/lina/Desktop/work/path.py"));
                byte[] buf = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = in.read(buf)) != -1) {
                    for (int i = 0; i < bytesRead; ++i) {
                        System.out.print((char) buf[i]);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test4() {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new FileInputStream("/Users/lina/Desktop/work/path.py");
                out = new FileOutputStream("/Users/lina/Desktop/work/out.py");
                byte[] buf = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = in.read(buf)) != -1) {
                    out.write(buf, 0, bytesRead);
                    out.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    
        @Test
        public void test5() {
            BufferedReader fr = null;
            try {
                fr = new BufferedReader(new FileReader("/Users/lina/Desktop/work/path.py"));
                String str = null;
                while ((str = fr.readLine()) != null) {
                    System.out.println(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test6() {
            InputStreamReader isr = null;
            try {
                isr = new InputStreamReader(new FileInputStream("/Users/lina/Desktop/work/path.py"));
                char[] ch = new char[1024];
                int len = 0;
                while ((len = isr.read(ch)) != -1) {
                    System.out.println(new String(ch, 0, len));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (isr != null) {
                    try {
                        isr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test7() {
            FileReader fr = null;
            try {
                fr = new FileReader("/Users/lina/Desktop/work/path.py");
                char[] ch = new char[1024];
                int len = 0;
                while ((len = fr.read(ch)) != -1) {
                    System.out.println(new String(ch, 0, len));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test8() {
            OutputStreamWriter osw = null;
            try {
                osw = new OutputStreamWriter(new FileOutputStream("/Users/lina/Desktop/test/d.txt"));
                char[] ch = new char[]{'我', '爱', '中', '国'};
                osw.write(ch);
                osw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (osw != null) {
                    try {
                        osw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test9() {
            FileWriter fw = null;
            try {
                fw = new FileWriter("/Users/lina/Desktop/test/d.txt");
                String str = "爱我中华";
                fw.write(str);
                fw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fw != null) {
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test10() {
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter("/Users/lina/Desktop/test/d.txt"));
                bw.write("爱我");
                bw.newLine();
                bw.write("中华");
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test11() {
            InputStreamReader isr = null;
            OutputStreamWriter osw = null;
            try {
                isr = new InputStreamReader(new FileInputStream("/Users/lina/Desktop/test/d.txt"));
                osw = new OutputStreamWriter(new FileOutputStream("/Users/lina/Desktop/test/f.txt"), "utf-8");
                char[] ch = new char[1024];
                int len = 0;
                while ((len = isr.read(ch)) != -1) {
                    osw.write(ch, 0, len);
                    osw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (isr != null) {
                    try {
                        isr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (osw != null) {
                    try {
                        osw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test12() {
            FileReader fr = null;
            FileWriter fw = null;
            try {
                fr = new FileReader("/Users/lina/Desktop/test/d.txt");
                fw = new FileWriter("/Users/lina/Desktop/test/c.txt");
                char[] ch = new char[1024];
                int len = 0;
                while ((len = fr.read(ch)) != -1) {
                    fw.write(ch, 0, len);
                    fw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fw != null) {
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test13() {
            BufferedReader br = null;
            BufferedWriter bw = null;
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/lina/Desktop/test/d.txt")));
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/Users/lina/Desktop/test/abc.txt")));
                char[] ch = new char[1024];
                int len = 0;
                while ((len = br.read(ch)) != -1) {
                    bw.write(ch, 0, len);
                    bw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void test14() {
            BufferedReader br = null;
            BufferedWriter bw = null;
            try {
                br = new BufferedReader(new FileReader("/Users/lina/Desktop/test/d.txt"));
                bw = new BufferedWriter(new FileWriter("/Users/lina/Desktop/test/ab.txt"));
                String str = null;
                while ((str = br.readLine()) != null) {
                    bw.write(str);
                    bw.newLine();
                    bw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    三星t5拆解
    一条 SQL 引发的事故,同事直接被开除!!
    Git 不能提交空目录?我也是醉了!
    Redis 6.0.8 紧急发布,请尽快升级!
    String.format() 图文详解,写得非常好!
    为什么 Redis 要比 Memcached 更火?
    Lambda 表达式入门,这篇够了!
    天啊,为什么我的 Redis 变慢了。。
    写出一手烂代码的 19 条准则!
    Redis 面试一定要知道的 3 个 问题!
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10327240.html
Copyright © 2011-2022 走看看