package fivecopy; import java.io.*; public class FileIo { public static void main(String[] args) throws IOException { method(); method1(); method2(); method3(); method4(); //后四种为字节复制 method5(); method6(); method7(); method8(); } private static void method8() throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream("yf.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("yf9.txt")); byte[] bytes = new byte[1024]; int length; while ((length = bis.read(bytes)) != -1) { bos.write(bytes, 0, length); bos.flush(); } bis.close(); bos.close(); } private static void method7() throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream("yf.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("yf8.txt")); int by; while ((by = bis.read()) != -1) { bos.write(by); } bis.close(); bos.close(); } private static void method6() throws IOException{ FileInputStream fis = new FileInputStream("yf.txt"); FileOutputStream fos = new FileOutputStream("yf7.txt"); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) != -1) { fos.write(bytes, 0, length); fos.flush(); } fos.close(); fis.close(); } private static void method5() throws IOException{ FileInputStream fis = new FileInputStream("yf.txt"); FileOutputStream fos = new FileOutputStream("yf6.txt"); int by; while ((by = fis.read()) != -1) { fos.write(by); } fos.close(); fis.close(); } private static void method4() throws IOException { BufferedReader br = new BufferedReader(new FileReader("yf.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("yf5.txt")); String s = null; while ((s = br.readLine()) != null) { bw.write(s); bw.newLine(); } br.close(); bw.close(); } private static void method3() throws IOException { BufferedReader br = new BufferedReader(new FileReader("yf.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("yf4.txt")); char[] chars = new char[1024]; int length; while ((length = br.read(chars)) != -1) { bw.write(chars, 0, length); bw.flush(); } bw.close(); br.close(); } private static void method2() throws IOException { BufferedReader br = new BufferedReader(new FileReader("yf.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("yf3.txt")); int ch; while ((ch = br.read()) != -1) { bw.write(ch); } bw.close(); br.close(); } private static void method1() throws IOException { FileReader fr = new FileReader("yf.txt"); FileWriter fw = new FileWriter("yf2.txt"); char[] chars = new char[1024]; int length; while ((length = fr.read(chars)) != -1) { fw.write(chars, 0, length); fw.flush(); } fr.close(); fw.close(); } // 字符流5个方法 复制 // 第一 OutputStreamWriter FileWriter InputStreamReader FileReader // BufferWriter BufferReader 特有方法 newLine private static void method() throws IOException { FileReader fr = new FileReader("yf.txt"); FileWriter fw = new FileWriter("yf1.txt"); int ch; while ((ch = fr.read()) != -1) { fw.write(ch); } fw.close(); fr.close(); } }
IO的九种复制
猜你喜欢
转载自www.cnblogs.com/yfinter/p/9348649.html
今日推荐
周排行