任务描述
本关任务:使用字节输出流和输入流,把给定文件里的内容复制到另一个给定文件中。
编程要求
仔细阅读右侧编辑区内给出的代码框架及注释,在 Begin-End 间编写程序代码,使用字节输出流和输入流,把给定文件里的内容复制到另一个给定文件中。具体要求如下:
- 接收给定的一行字符串(如:/test1/d.txt /test1/e.txt。其中第一个路径为源文件路径,第二个路径为目标文件路径);
- 使用字节输出流和输入流,把第一个文件里的内容复制到第二个文件中。
import java.io.*;
import java.util.Scanner;
public class FileTest {
public static void main(String[] args) throws IOException {
// 请在Begin-End间编写完整代码
/********** Begin **********/
// 使用字节输出流和输入流,把给定文件里的内容复制到另一个给定文件中
Scanner input = new Scanner(System.in);
String str1 = input.next();
String str2 = input.next();
FileInputStream fileInputStream = new FileInputStream(str1);
FileOutputStream fileOutputStream1 = new FileOutputStream(str2);
int len;
while ((len = fileInputStream.read()) != -1){
fileOutputStream1.write(len);
}
fileInputStream.close();
fileOutputStream1.close();
/********** End **********/
}
}