缓冲字符流BufferedInputStream / BufferedOutputStream
文件操作流 InputStream / OutputStream
java 输入输出流的套接,测试一下流链的速度
因为节点流在程序中并不是很常用,一般常通过过滤流将多个流套接在一起,利用各流的特性共同处理数据,套接的多个流构成了一个流链;
我们来分析他们的运行时间。(因为在不同的系统(maxOX,Linux,WIN(32/64))上运行的时间可能都不相同)我截取了几段时间来进行对比。
1.缓冲字符流BufferedInputStream / BufferedOutputStream
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { long StartTime = System.nanoTime(); File address = new File("D:"+File.separator+"c.txt"); File address1 = new File("D:"+File.separator+"ww.txt"); BufferedInputStream bin = new BufferedInputStream(new FileInputStream(address)); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(address1)); int c ; while((c=bin.read())!=-1) { bout.write(c); } bin.close(); bout.flush(); bout.close(); System.out.println("复制完成"); long endTime = System.nanoTime(); System.out.println((endTime-StartTime)+"ms"); } }
2.文件操作流 InputStream / OutputStream
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void main(String[] args)throws IOException { long StartTime = System.nanoTime(); File address = new File("D:"+File.separator+"c.txt"); File address1 = new File("D:"+File.separator+"ww.txt"); InputStream in = new FileInputStream(address); OutputStream out = new FileOutputStream (address1); int c ; while((c=in.read())!=-1) { out.write(c); } in.close(); out.flush(); out.close(); System.out.println("复制完成"); long endTime = System.nanoTime(); System.out.println((endTime-StartTime)+"ms"); } }
3.java 输入输出流的套接
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void main(String[] args) throws IOException { long StartTime = System.nanoTime(); File address = new File("D:"+File.separator+"c.txt"); File address1 = new File("D:"+File.separator+"ww.txt"); BufferedInputStream bin = new BufferedInputStream(new FileInputStream(address)); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(address1)); InputStream din = new DataInputStream(bin); OutputStream dout = new DataOutputStream(bout); int c ; while((c = din.read())!=-1) { dout.write(c); } bin.close(); bout.flush(); bout.close(); din.close(); dout.flush(); dout.close(); System.out.println("复制成功"); long endTime = System.nanoTime(); System.out.println((endTime-StartTime)+"ms"); } }
他们的运行时间在某种程度上是类似的,因为他们每次只读取一个字符,然后写入到文件中。
他们的使用需要看场景选择:
普通的文件输入输出时 选择 FileInputStream类
在需要更快的输入输出时选择 Buffering类
在多重输入输出时选择Java 输入输出流的套接,可以方便处理数据并提高处理的效率。
原文地址:https://www.cnblogs.com/algorithmvcerhj/p/9094973.html
时间: 2024-11-12 16:30:31