You are on page 1of 5

National Aviation University

Faculty of Cybersecurity, Computer and Software Engineering


Software engineering department

Software development studio

Laboratory work 2.3

VARIANT № 15

Prepared by:
student of SE-227 en.,
Frynas Vadym

Accepted by:
Tkachenko O.A.
FileResults.java
package com.company;

import java.util.ArrayList;

public class FileResults {


String name;
ArrayList<Integer> realValues;
FileResults(String name,ArrayList<Integer> realValues)
{
this.name=name;
this.realValues=realValues;
}

@Override
public String toString() {
String str="File name:\n"+name+"\nAmount of integer in this file -
"+realValues.size()+ ":\n";
for (var item:
realValues) {
str+=item+" ";
}
return str+"\n";
}
}
MyThread.java
package com.company;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;

public class MyTread extends Thread{


public final String inputPath;
public final String outputPath;
ArrayList<FileResults> results;
public MyTread(String inputPath, String outputPath){
this.inputPath = inputPath;
this.outputPath = outputPath;
}

@Override
public void run() {
results=new ArrayList<FileResults>();
File file = new File(inputPath);
System.out.println("Reading from file " + file.getName());
ArrayList<Integer> realValues=new ArrayList<Integer>();
try(BufferedReader br=new BufferedReader(new FileReader(file))) {
String data = "";
while ((data=br.readLine())!=null)
{
String[] words=data.split(" ");
for (var item1:
words) {
if(tryParseInt(item1))
{
realValues.add(Integer.parseInt(item1));
}
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
results.add(new FileResults(inputPath,realValues));

System.out.println("Creating file "+ outputPath+"\\New" +


file.getName());
File newFile = new File(outputPath+"\\New" + file.getName());
try {
newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
var outputFile = outputPath+"\\New" + file.getName();
System.out.println("Writing to file in "+ outputFile);
for (var item :
results) {try {
Files.writeString(Path.of(outputFile),item.toString());
} catch (IOException e) {
e.printStackTrace();
}

}
}

boolean tryParseInt(String value) {


try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
Main.java
package com.company;
import java.io.File;
import java.util.HashSet;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {


while (true){
System.out.println("Enter path to the input directory");
Scanner scanner = new Scanner(System.in);
var dirPath = scanner.nextLine();
var dir = new File(dirPath);
var filePaths = new HashSet<String>();
if (dir.exists()&&dir.isDirectory()){
filePaths = GetAllFiles(dirPath);
}
else {
System.out.println("Directory doesn't exist or invalid
path");
continue;
}
System.out.println("Enter the valid path to the output
directory");
var outputPath = scanner.nextLine();
var dirOut = new File(outputPath);
if (dirOut.exists()){
if (!dirOut.isDirectory()){
System.out.println("This is file path. Please enter path
to directory");
continue;
}
}
else {
System.out.println("Directory doesn't exist or invalid
path");
continue;
}
MyTread FileThreads[] = new MyTread[filePaths.size()];
int i =0;
for (var file: filePaths) {
FileThreads[i] = new MyTread(file,outputPath);
FileThreads[i].start();
i++;
}
for (int j = 0; j< filePaths.size();j++) {
try {
FileThreads[j].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
}
}public static void GetFilesFromDir(HashSet<String> files, File dir){
for (var file : dir.listFiles()){
if(file.isDirectory()){
GetFilesFromDir(files,file);
}
else {
if(file.getName().endsWith(".txt")){
files.add(file.getAbsolutePath());
}
}
}
}
public static HashSet<String> GetAllFiles(String folderPath){
var filePaths = new HashSet<String>();
var folder = new File(folderPath);
GetFilesFromDir(filePaths,folder);
return filePaths;
}
}

Part of program execution


Results

You might also like