这个题是不是有歧义,是要第一次输入的时候没有文件名的时,复制第一个文件的内容,还是输出文件不存在。如果没有文件名如何知道要复制那个文件?
package zh.codegym.task.task09.task0929;
import java.io.*;
/*
让代码发挥它的用武之地!
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sourceFileName = reader.readLine();
String destinationFileName = reader.readLine();
try{
InputStream fileInputStream = getInputStream(sourceFileName);
OutputStream fileOutputStream = getOutputStream(destinationFileName);
while (fileInputStream.available() > 0) {
int data = fileInputStream.read();
fileOutputStream.write(data);
}
fileInputStream.close();
fileOutputStream.close();
}
catch(Exception e){
System.out.println("文件不存在");
while(sourceFileName == null) {
sourceFileName = reader.readLine();
destinationFileName = reader.readLine();
}
InputStream fileInputStream = getInputStream(sourceFileName);
OutputStream fileOutputStream = getOutputStream(destinationFileName);
while (fileInputStream.available() > 0) {
int data = fileInputStream.read();
fileOutputStream.write(data);
}
fileInputStream.close();
fileOutputStream.close();
}
}
public static InputStream getInputStream(String fileName) throws IOException {
return new FileInputStream(fileName);
}
public static OutputStream getOutputStream(String fileName) throws IOException {
return new FileOutputStream(fileName);
}
}