缓冲IO流
缓冲流
1.BufferedInputStream:缓冲字节输入流
2.BufferedOutPutStream:缓冲字节输出流
3.BufferedReader:缓冲字符输入流
4.BufferedWriter:缓冲字符输出流
package file; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //复制文件 public class test05 { public static void main(String[] args) throws IOException { copyFile("E:/java/read.txt","E:/java/hello.txt"); } //复制文件 public static void copyFile(String srcPath,String destPath) throws IOException { //选择IO流 //1.从srcPath文件读取:FileInputStream //2.把数据写到destPath ;FileOutputStream FileInputStream fis = new FileInputStream(srcPath); FileOutputStream fos= new FileOutputStream(destPath); //一边读一边写 byte[] data=new byte[1024]; int len; while((len=fis.read(data))!=-1) { //fos.write(data);//最后一次,可能遗留了上次的内容 fos.write(data,0,len); } //关闭IO流 fos.close(); fis.close(); } }
package file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* 缓冲流IO流 1.BufferedInputStream:缓冲字节输入流 2.BufferedOutPutStream:缓冲字节输出流 3.BufferedReader:缓冲字符输入流 4.BufferedWriter:缓冲字符输出流 数据的流向:srcPath-->fis-->bis-->字节数组-->bos-->fos-->destPath */ public class test06 { //复制大文件 public static void main(String[] args) throws IOException { copyFile("E:/java/read.txt","E:/java/hello.txt"); } public static void copyFile(String srcPath,String destPath) throws IOException { //选择IO流 FileInputStream fis = new FileInputStream(srcPath); FileOutputStream fos= new FileOutputStream(destPath); //缓冲区提高效率 BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos=new BufferedOutputStream(fos); //一边读一边写 byte[] data=new byte[1024]; int len; while((len=bis.read(data))!=-1) { //fos.write(data);//最后一次,可能遗留了上次的内容 bos.write(data,0,len); } //关闭IO流 bis.close(); fis.close(); bos.close(); fos.close(); } }
package file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* 传统try...catch try{} catch(异常类型 e){} finally{资源关闭} try...with..resource try(资源的申明){} catch(异常类型 e){} */ public class test07 { public static void main(String[] args) throws IOException { copyFile("E:/java/read.txt","E:/java/world.txt"); } public static void copyFile(String srcPath,String destPath){ try( FileInputStream fis = new FileInputStream(srcPath); FileOutputStream fos= new FileOutputStream(destPath); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos=new BufferedOutputStream(fos); ) { byte[] data=new byte[1024]; int len; while((len=bis.read(data))!=-1) { //fos.write(data);//最后一次,可能遗留了上次的内容 bos.write(data,0,len); } }catch(FileNotFoundException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } }
package file; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /*BufferedReader:readLine() 纯文本数据*/ public class test08 { public static void main(String[] args) throws IOException { //创建IO流 BufferedReader br=new BufferedReader(new FileReader("E:/java/hello.txt")); //读取 String line; while((line=br.readLine())!=null) { System.out.println(line); } //关闭 br.close(); } }
posted on 2020-06-05 15:57 happygril3 阅读(192) 评论(0) 收藏 举报
浙公网安备 33010602011771号