-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyFile.java
89 lines (77 loc) · 2.31 KB
/
CopyFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.javamultiplex.filehandling;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CopyFile {
public static void main(String[] args) throws IOException {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter source file name with extension : ");
String sourceFileName = input.nextLine();
if (isValidFileName(sourceFileName)) {
File sourceFile = new File(sourceFileName);
if (sourceFile.exists()) {
System.out.println("Enter destination file name with extension : ");
String destinationFileName = input.nextLine();
if (isValidFileName(destinationFileName)) {
File destinationFile = new File(destinationFileName);
if (isFileCopied(sourceFile, destinationFile)) {
System.out.println("File copied successfully.");
} else {
System.out.println("Some error occurred while copying file.");
}
} else {
System.out .println("Destination file name is not valid.");
}
} else {
System.out.println("Source file doesn't exist in current directory.");
}
} else {
System.out.println("Source file name is not valid.");
}
} finally {
if (input != null) {
input.close();
}
}
}
private static boolean isFileCopied(File sourceFile, File destinationFile)
throws IOException {
BufferedReader in = null;
PrintWriter out = null;
boolean result = true;
try {
FileReader fr = new FileReader(sourceFile);
in = new BufferedReader(fr);
out = new PrintWriter(destinationFile);
String temp = in.readLine();
while (temp != null) {
out.println(temp);
temp = in.readLine();
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
result = false;
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
return result;
}
private static boolean isValidFileName(String fileName) {
// Regular expression for validating file names.
String pattern = "^.+\\..+$";
boolean result = false;
if (fileName.matches(pattern)) {
result = true;
}
return result;
}
}