用FileInputStream和FileOutputStream来实现一个文件移动程序,并测试移动1G文件所需要的时间。
时间: 2024-04-30 11:19:23 浏览: 14
以下是Java代码实现文件移动程序,并计算移动1G文件所需的时间:
```java
import java.io.*;
public class FileMoveExample {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
File sourceFile = new File("sourceFile.txt");
File targetFile = new File("targetFile.txt");
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
if (sourceFile.delete()) {
System.out.println("Source file moved successfully!");
} else {
System.out.println("Failed to move source file!");
}
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
System.out.println("Time taken to move file: " + timeElapsed + " milliseconds");
}
}
```
测试移动1G文件所需的时间:
```java
import java.io.*;
public class FileMoveExample {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
File sourceFile = new File("sourceFile.txt");
File targetFile = new File("targetFile.txt");
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
if (sourceFile.delete()) {
System.out.println("Source file moved successfully!");
} else {
System.out.println("Failed to move source file!");
}
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
System.out.println("Time taken to move file: " + timeElapsed + " milliseconds");
}
}
```
注意:测试移动1G文件所需的时间可能需要等待较长时间,取决于计算机性能和硬盘速度。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)