Java -- Buffered

xiaoxiao2021-02-28  7

public class TestBuffered { public static void main(String[] args) { //使用缓冲流实现费文本文件的复制 FileInputStream f=null; FileOutputStream f2=null; try { //提供读入,写出的文件 File file=new File("hello.jpg"); File file2=new File("hello2.jpg"); //创建相应的节点流 f = new FileInputStream("file"); f2 = new FileOutputStream("file2"); //将创建的节点流的对象作为形参传递给缓冲流的构造器 BufferedInputStream inputStream = new BufferedInputStream(f); BufferedOutputStream outputStream = new BufferedOutputStream(f2); //实现文件的复制 byte[] b=new byte[1024]; int len; while((len=inputStream.read(b))!=-1){ outputStream.write(b,0,len); outputStream.flush();//刷新 } } catch (IOException e) { e.printStackTrace(); //关闭流 }finally{ try { f.close(); } catch (IOException e) { e.printStackTrace(); } try { f2.close(); } catch (IOException e) { e.printStackTrace(); } } test(); } private static void test() { BufferedReader br=null; BufferedWriter bw=null; try { File file=new File("hello.txt"); File file2=new File("hello2.txt"); FileReader fr=new FileReader(file); FileWriter fw = new FileWriter(file2); br=new BufferedReader(fr); // char[] c=new char[1024]; // int len; // while((len=br.read(c))!=-1){ // String s=new String(c,0,len); // System.out.println(s); // } String str=null; while((str=br.readLine())!=null){ // System.out.println(str); bw.write(str); bw.newLine();//换行 bw.flush(); } } catch (IOException e) { e.printStackTrace(); }finally{ if(bw!=null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
转载请注明原文地址: https://www.6miu.com/read-1100094.html

最新回复(0)