-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_multiple_files.java
More file actions
61 lines (54 loc) · 2.58 KB
/
Copy pathMerge_multiple_files.java
File metadata and controls
61 lines (54 loc) · 2.58 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
import java.io.*;
import java.util.*;
/*
Собираем файл из других файлов, лежащих в одной папке
*/
public class Merge_multiple_files {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the files path: ");
String directory_name = reader.readLine(); // Считываем имя папки, в которой лежат файлы для объединения
reader.close();
File listFile = new File(directory_name);
File exportFiles[] = listFile.listFiles();
String[] names = new String[exportFiles.length]; // Создаем массив names
for (int i = 0; i < names.length; i++) {
names[i] = exportFiles[i].getName(); // Заполняем массив names именами файлов, лежащих в папке
}
Arrays.sort(names, new Comparator<String>() { // Сортируем массив names
@Override
public int compare(String o1, String o2) {
int n1 = extractNumber(o1);
int n2 = extractNumber(o2);
return n1 - n2;
}
private int extractNumber(String name) {
int i = 0;
try {
int s = name.indexOf('_') + 1;
int e = name.lastIndexOf('.');
String number = name.substring(s, e);
i = Integer.parseInt(number);
} catch (Exception e) {
i = 0; // если имя файла не соответствует формату, то по умолчанию 0
}
return i;
}
});
try {
String mainFile = directory_name + "/join_file.txt";
File file = new File(mainFile);
FileOutputStream outputStream = new FileOutputStream(mainFile);
for (int i = 0; i < names.length; i++) {
FileInputStream inputStream = new FileInputStream(directory_name + "/" + names[i]);
byte[] buffer = new byte[inputStream.available()]; // Читаем весь файл одним куском
inputStream.read(buffer);
inputStream.close();
outputStream.write(buffer);
}
outputStream.close();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}