【发布时间】:2018-08-13 07:48:48
【问题描述】:
我想知道哪个更快。序列输入流或文件输入流。 这是我的示例程序
FileInputStream fileInput = new FileInputStream("C:\\Eclipse\\File Output Stream.txt");
FileInputStream fileInput1=new FileInputStream("C:\\Eclipse\\Buffer Output Stream.txt");
SequenceInputStream sequence=new SequenceInputStream(fileInput,fileInput1);
FileOutputStream outputSequenceStream=new FileOutputStream("C:\\Eclipse\\File OutputSequence Stream.txt");
int i=0;
byte[] b = new byte[10];
long start=System.currentTimeMillis();
//System.out.println(start);
while((i=sequence.read())!=-1){
//outputSequenceStream.write(i);
System.out.println(Integer.toBinaryString(i)+" "+i+" "+ (char)i);
}
System.out.println(System.currentTimeMillis()-start);
System.out.println("success");
System.out.println("Reading one file after another using file input");
FileInputStream fileInput2 = new FileInputStream("C:\\Eclipse\\File Output Stream.txt");
FileInputStream fileInput3=new FileInputStream("C:\\Eclipse\\Buffer Output Stream.txt");
start=System.currentTimeMillis();
/* Reading first file */
while((i=fileInput2.read())!=-1){
System.out.println((char)i);
}
/* Reading second file */
while((i=fileInput3.read())!=-1){
System.out.println((char)i);
}
System.out.println(System.currentTimeMillis()-start);
System.out.println("Success");
文件输入流给我的数字比序列输出流少。这是否意味着序列比文件输入流慢。如果是这样,那么为什么我们使用序列流而不是使用文件输入流不是更好吗?
【问题讨论】:
-
郑重声明:您希望其他人花时间帮助您解决问题。所以请您花 1 分钟时间来正确格式化/缩进您的代码!缩进很重要,并且缺乏适当的缩进(连同无意义的变量名)会使您的输入非常“没有人愿意看它”。
-
然后:请考虑在某个时候接受有用的答案。似乎你一直在问问题,但很少接受答案......
-
如果你关心性能,你根本不应该一个字节一个字节地传输文件。考虑
try(FileChannel in1 = FileChannel.open(Paths.get("C:\\Eclipse\\File Output Stream.txt"), StandardOpenOption.READ); FileChannel in2 = FileChannel.open(Paths.get("C:\\Eclipse\\Buffer Output Stream.txt"), StandardOpenOption.READ); FileChannel out = new FileOutputStream(FileDescriptor.out).getChannel()) { in1.transferTo(0, in1.size(), out); in2.transferTo(0, in2.size(), out); }