package cn.tetu; import java.io.*; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Scanner; public class 复习 { public static void main(String[] args) { // f1();//字节输入流 // f2();//字符输入流 //f3();//字节输出流 // f4();//字符输出流 // f5();//字符流复制文件内容 f6();//字节流复制文件内容 } private static void f6() { System.out.println("请输入文件名"); String a = new Scanner(System.in).nextLine(); System.out.println("请输入被打印文件的路径"); String b = new Scanner(System.in).nextLine(); BufferedInputStream c = null; BufferedOutputStream d = null; try { c=new BufferedInputStream(new FileInputStream(a)); d = new BufferedOutputStream(new FileOutputStream(b)); int e ; while ((e= c.read())!=-1){ d.write(e); } System.out.println("复制成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("复制失败"); }finally { try { d.close(); } catch (IOException e) { e.printStackTrace(); } try { c.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void f5() { System.out.println("请输入文件名"); String a = new Scanner(System.in).nextLine(); System.out.println("请输入被打印文件的路径"); String b = new Scanner(System.in).nextLine(); BufferedReader c = null; BufferedWriter d = null; try { c = new BufferedReader(new FileReader(a)); d = new BufferedWriter(new FileWriter(b)); int e ; while ((e=c.read())!=-1){ d.write(e); } System.out.println("复制成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("复制失败"); }finally { try { d.close(); } catch (IOException e) { e.printStackTrace(); } try { c.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void f4() { BufferedWriter a = null; try { a = new BufferedWriter(new FileWriter("D:\\\\ready\\\\2.txt",true)); a.write(98); } catch (Exception e) { e.printStackTrace(); }finally { try { a.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void f3() { BufferedOutputStream a =null; try { a = new BufferedOutputStream(new FileOutputStream("D:\\\\ready\\\\2.txt")); a.write(97); a.write(98); a.write(99); a.write(99); } catch (Exception e) { e.printStackTrace(); }finally { try { a.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void f2() { BufferedReader a=null; try { a = new BufferedReader(new FileReader("D:\\\\ready\\\\1.txt")); int b ; while ((b=a.read())!=-1){ System.out.println(b); } } catch (Exception e) { e.printStackTrace(); }finally { try { a.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void f1() { BufferedInputStream a=null; try { a = new BufferedInputStream(new FileInputStream("D:\\\\ready\\\\1.txt")); int b ; while ((b=a.read())!=-1){ System.out.println(b); } } catch (Exception e) { e.printStackTrace(); }finally { try { a.close(); } catch (IOException e) { e.printStackTrace(); } } } }
流:字节输入流,字符输入流,字节输出流,字符输出流,coppy案例
最新推荐文章于 2024-08-23 00:00:00 发布