字节流和字符流

 

 

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

 

转载于:https://www.cnblogs.com/xidian2014/p/10327240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值