码迷,mamicode.com
首页 > 其他好文 > 详细

字节流和字符流

时间:2019-01-27 19:23:07      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:sre   desktop   技术分享   爱我   tst   ace   NPU   lin   writer   

 

技术分享图片

 

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

 

字节流和字符流

标签:sre   desktop   技术分享   爱我   tst   ace   NPU   lin   writer   

原文地址:https://www.cnblogs.com/xidian2014/p/10327240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!