public class Solution {
public static void main(String[] args) throws IOException {
File path = new File(args[0]);
File resultFileAbsolutePath = new File(args[1]);
File newFile = new File(resultFileAbsolutePath.getParent() + "/allFilesContent.txt");
FileUtils.renameFile(resultFileAbsolutePath, newFile);
Set<String> set = new TreeSet<>();
Files.walkFileTree(path.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException {
if (Files.isRegularFile(path)) {
if (basicFileAttributes.size() <= 50) {
// Files.write(newFile.toPath(), Files.readAllBytes(path), StandardOpenOption.APPEND);
set.add(path.getFileName().toString());
}
}
return FileVisitResult.CONTINUE;
}
});
for (String eachFile : set) {
try(BufferedInputStream reader = new BufferedInputStream(new FileInputStream(eachFile));
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newFile, true))){
while (reader.available()>0){
byte[] b = new byte[reader.read()];
writer.write(b);
}
writer.write("\n".getBytes());
}
catch (IOException e){
e.getStackTrace();
}
}
}
}
package com.javarush.task.task31.task3101;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/*
Проход по дереву файлов
*/
public class Solution {
public static void main(String[] args) throws IOException {
File path = new File(args[0]);
File resultFileAbsolutePath = new File(args[1]);
File newFile = new File(resultFileAbsolutePath.getParent() + "/allFilesContent.txt");
FileUtils.renameFile(resultFileAbsolutePath, newFile);
Set<String> set = new TreeSet<>();
// File path = new File("D:\\!QATestLab");
// File newFile = new File("d:\\allFilesContent.txt");
Files.walkFileTree(path.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException {
if (Files.isRegularFile(path)) {
if (basicFileAttributes.size() <= 50) {
// Files.write(newFile.toPath(), Files.readAllBytes(path), StandardOpenOption.APPEND);
set.add(path.getFileName().toString());
}
}
return FileVisitResult.CONTINUE;
}
});
for (String eachFile : set) {
try(BufferedInputStream reader = new BufferedInputStream(new FileInputStream(eachFile));
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newFile, true))){
while (reader.available()>0){
byte[] b = new byte[reader.read()];
writer.write(b);
}
writer.write("\n".getBytes());
}
catch (IOException e){
e.getStackTrace();
}
}
// try {
// Files.write(Paths.get("d:\\1.txt"), setFile);
// } catch (IOException e) {
// e.printStackTrace();
// }
// for (String fileName : solution.listFiles) {
// try (BufferedWriter writer = new BufferedWriter(new FileWriter(newFile, true));
// BufferedReader reader = new BufferedReader(new FileReader(fileName))){
// String tmp = reader.readLine();
// writer.write(tmp + "\n");
// } catch (IOException e) {
// e.getStackTrace();
// }
// }
// try(DirectoryStream<Path> entries = Files.newDirectoryStream(path)){
// for (Path entry : entries) {
// if(path.toFile().length() > 50000000) {
// setFile.add(entry.getFileName().toString());
// }
// }
// }
// catch (IOException e){
// e.getStackTrace();
// }
}
}