字节流和字符流


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

优质内容筛选与推荐>>
1、二分查找(返回相同元素索性最小)
2、Linux date命令
3、[转]远程唤醒技术在运维中的应用
4、MOSS通过页面查看错误信息
5、测试环境服务器windows server 2003资源下载


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号