Repository navigation
Expand file tree
/
Copy pathFile.java
More file actions
97 lines (82 loc) · 2.81 KB
/
Copy pathFile.java
File metadata and controls
97 lines (82 loc) · 2.81 KB
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
90
91
92
93
94
95
96
97
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class File {
// <editor-fold defaultstate="collapsed" desc="Constructors">
public File(){
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Queries">
public String loadFile(String path)
throws FileNotFoundException, IOException{
StringBuffer result = new StringBuffer();
BufferedReader in = null;
try{
FileReader reader = new FileReader(path);
in = new BufferedReader(reader);
String line = in.readLine();
while(line != null){
result.append(line + "\n");
line = in.readLine();
}
}
finally{
if(in != null){
try{
in.close();
}
catch(IOException ex){
//Reader close failed.
}
}
}
return result.toString();
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Commands">
public void mergeFiles(String[] sourcePaths, String targetPath)
throws FileNotFoundException, IOException{
ArrayList<String> lines = new ArrayList<String>();
BufferedReader in = null;
PrintWriter out = null;
try{
for(String sourcePath : sourcePaths){
FileReader reader = new FileReader(sourcePath);
in = new BufferedReader(reader);
String line = in.readLine();
while(line != null){
lines.add(line);
line = in.readLine();
}
try{
in.close();
}
catch(IOException ex){}
}
//Store the lines in an array list, so that if something goes wrong
// with one of the files, then the target is not created if it wont
// be used.
FileWriter writer = new FileWriter(targetPath);
out = new PrintWriter(writer);
for(String mergeLine : lines){
out.println(mergeLine);
}
}
finally{
try{
if(in != null){
in.close();
}
}
catch(IOException ex){ }
if(out != null){
out.close();
}
}
}
// </editor-fold>
}