请老师帮忙看看问题出在哪里
来源:3-16 自由编程
weixin_慕斯0354824
2020-02-22 18:07:35
package com.imooc.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class BufferedStreamDemo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
long time1=0;
long time2=0;
long time=0;
long time0=0;
try {
File f=new File("e:\\JavaWorkSpace\\IoFileSpace\\org.txt");
File f0=new File("e:\\JavaWorkSpace\\IoFileSpace\\org0.txt");
File f1=new File("e:\\JavaWorkSpace\\IoFileSpace\\one.txt");
File f2=new File("e:\\JavaWorkSpace\\IoFileSpace\\two.txt");
//原始文件org的输入、输出流
FileOutputStream fos=new FileOutputStream("e:\\JavaWorkSpace\\IoFileSpace\\org.txt");
FileInputStream fis=new FileInputStream("e:\\JavaWorkSpace\\IoFileSpace\\org.txt");
FileOutputStream fos0=new FileOutputStream("e:\\JavaWorkSpace\\IoFileSpace\\org0.txt");
FileInputStream fis0=new FileInputStream("e:\\JavaWorkSpace\\IoFileSpace\\org0.txt");
//org0用缓冲的文件输出流
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos0=new BufferedOutputStream(fos0);
//one、two的输出流
FileOutputStream fos1=new FileOutputStream("e:\\JavaWorkSpace\\IoFileSpace\\one.txt");
FileOutputStream fos2=new FileOutputStream("e:\\JavaWorkSpace\\IoFileSpace\\two.txt");
//two的缓冲输出流
BufferedOutputStream bos=new BufferedOutputStream(fos2);
//判断原始文件org是否存在,若不存在则创建文件,然后写入数据
while(true) {
if(!f.exists()) {
f.createNewFile();
System.out.println("原文件生成完毕");
continue;
}
if(!f0.exists()) {
f0.createNewFile();
System.out.println("文件0生成完毕");
continue;
}
if(!f1.exists()) {
f1.createNewFile();
System.out.println("文件1生成完毕");
continue;
}
if(!f2.exists()) {
f2.createNewFile();
System.out.println("文件2生成完毕");
continue;
}
else {
long startTime=System.currentTimeMillis();
int i=0;
while(i<1000000) {
fos.write('0');
i++;
}
long endTime=System.currentTimeMillis();
time0=endTime-startTime;
System.out.println("org所耗时间:"+time);
long startTime0=System.currentTimeMillis();
int i0=0;
while(i0<1000000) {
bos0.write('0');
i0++;
}
bos0.flush();
bos0.close();
fis0.close();
long endTime0=System.currentTimeMillis();
time0=endTime0-startTime0;
System.out.println("org0所耗时间:"+time0);
System.out.println("节省时间:"+(time-time0));
System.out.println("数据写入完毕,是否开始生成拷贝?");
System.out.println("1开始拷贝/0退出程序");
int c=0;
Scanner sc=new Scanner(System.in);
try {
c=sc.nextInt();
} catch (java.util.InputMismatchException e) {
// TODO 自动生成的 catch 块
System.out.println("输入有误!请重新输入数字!");
sc.next();
continue;
}
if(c==1) {
//复制粘贴org至one,计时
int n1=0;
byte b1[]=new byte[i];
long startTime1=System.currentTimeMillis();
if((n1=fis.read())!=-1) {
fos1.write(b1);
}
long endTime1=System.currentTimeMillis();
fos.flush();
time1=endTime1-startTime1;
System.out.println("粘贴至one所耗时间:"+time1);
fos1.close();
////复制粘贴org至two,计时
int n2=0;
byte b2[]=new byte[i];
long startTime2=System.currentTimeMillis();
if((n2=bis.read())!=-1) {
bos.write(b2);
}
long endTime2=System.currentTimeMillis();
time2=endTime2-startTime2;
bos.flush();
fis.close();
fos.close();
bos.close();
System.out.println("粘贴至two所耗时间:"+time2);
System.out.println("节省时间:"+(time1-time2));
System.exit(0);
}
else if(c==0) {
System.out.println("==程序结束==");
System.exit(0);
}
else {
System.out.println("输入的数字有误!请重新输入!");
continue;
}
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}运行结果为:
org所耗时间:0
org0所耗时间:31
节省时间:-31
数据写入完毕,是否开始生成拷贝?
1开始拷贝/0退出程序
1
粘贴至one所耗时间:1
粘贴至two所耗时间:1
节省时间:0
1回答
同学你好,之所以节省时间为负数,是因为同学运算有误。修改如下:

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题