• 字节流和字符流


    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();
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    Essential Phone PH1官方刷机方法
    科普一下bl锁的知识,没解锁的必看!
    谷歌pixel手机解BL锁、刷机、破解电信(史上最详细的帖子)
    Fiddler4入门——手机抓包
    Windows10远程报错:由于CredSSP加密Oracle修正
    简单的利用JS来判断页面是在手机端还是在PC端打开的方法
    C# 之 比较两个word文档的内容
    Browsers 之 弹出窗口阻止问题
    Asp.Net 之 二维码生成
    windows Server2012 之 IIS8.0配置安装完整教程
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10327240.html
Copyright © 2020-2023  润新知