package com.cast.io;
import java.io.*;
import java.time.LocalDateTime;
import java.util.Scanner;
public class EasyCopy {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please write sourceFileName and path,such as C:\\xxx.txt");
String sourceFileName = scanner.nextLine();
System.out.println("Please write targetFileName and path,such as C:\\xxx.txt");
String targetFileName = scanner.nextLine();
scanner.close();
File file = new File(sourceFileName);
File file2 = new File(targetFileName);
if (file.exists() && file2.exists()) {
copyStart(sourceFileName, targetFileName);
System.out.println("Start Method one");
} else if (file.exists() && file2.exists() == false) {
copyStart2(sourceFileName, targetFileName);
System.out.println("start Method 2");
} else {
System.out.println("没有源文件");
}
}
public static void copyStart2(String sourceSrc, String targetSrc) {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
new File(targetSrc).createNewFile();
} catch (IOException e) {
System.out.println("已经有了");
}
try {
fis = new FileInputStream(sourceSrc);
fos = new FileOutputStream(targetSrc);
byte[] bytes = new byte[500];
int num = 0;
while ((num = fis.read(bytes)) != -1) {
fos.write(bytes, 0, num);
}
fos.write(("\r\nlastChangeTime" + LocalDateTime.now()).getBytes());
} catch (FileNotFoundException e) {
System.out.println("没能找到这个文件");
} catch (IOException e) {
System.out.println("读写报错");
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void copyStart(String sourceSrc, String targetSrc) {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(sourceSrc);
fos = new FileOutputStream(targetSrc);
byte[] bytes = new byte[500];
int num = 0;
while ((num = fis.read(bytes)) != -1) {
fos.write(bytes, 0, num);
}
fos.write(("\r\nlastChangeTime" + LocalDateTime.now()).getBytes());
} catch (FileNotFoundException e) {
System.out.println("没能找到这个文件");
} catch (IOException e) {
System.out.println("读写报错");
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}