import java.io.*;
public class FileCopy {
public static void main(String[] args) {
fileCopy("file\\source", "file\\source");
fileCopy2("file\\src", "file\\src");
}
private static boolean fileCopy(String srcPath, String dstPath) {
if (checkExists(dstPath)) {
return false;
}
try (InputStream inputStream = new FileInputStream(srcPath);
OutputStream outputStream = new FileOutputStream(dstPath)) {
byte[] array = new byte[1024];
int length = 0;
while ((length = inputStream.read(array)) != -1) {
outputStream.write(array, 0, length);
}
outputStream.flush();
return true;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static boolean fileCopy2(String srcPath, String dstPath) {
if (checkExists(dstPath)) {
return false;
}
try (Reader reader = new FileReader(srcPath); Writer writer = new FileWriter(dstPath)) {
char[] array = new char[100];
int length = 0;
while ((length = reader.read(array)) != -1) {
writer.write(array, 0, length);
}
writer.flush();
return true;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static boolean checkExists(String path) {
return new File(path).exists();
}
}