Created
December 7, 2019 09:21
-
-
Save monjasa/21b2bd3084ab1e31dc3b6fd2c57683bf to your computer and use it in GitHub Desktop.
file_task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/file_task.iml" filepath="$PROJECT_DIR$/file_task.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class FileClient { | |
private static String directoryName; | |
private static String fileName; | |
private static File file = null; | |
public static void main(String[] args) { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
try { | |
System.out.println("enter the name of the file: "); | |
fileName = reader.readLine(); | |
System.out.println("enter the name of the root directory: "); | |
directoryName = reader.readLine(); | |
} catch (IOException exception) { | |
exception.printStackTrace(); | |
} | |
Thread thread = new Thread(new FileFounder(fileName, directoryName)); | |
thread.start(); | |
try { | |
thread.join(); | |
} catch (InterruptedException exception) { | |
exception.printStackTrace(); | |
} | |
if (file != null) { | |
System.out.println("\nfile " + fileName + " has been found, the full path: " + file.getAbsolutePath()); | |
} else { | |
System.out.println("\nfile " + fileName + " has not been found in all subdirectories of " + directoryName); | |
} | |
} | |
public static File getFile() { | |
return file; | |
} | |
public static void setFile(File file) { | |
FileClient.file = file; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
public class FileFounder implements Runnable { | |
private String fileName; | |
private String directoryName; | |
public FileFounder(String fileName, String directoryName) { | |
this.fileName = fileName; | |
this.directoryName = directoryName; | |
} | |
@Override | |
public void run() { | |
if (FileClient.getFile() != null) | |
return; | |
if (Files.isDirectory(Paths.get(directoryName))) { | |
System.out.println("directory " + directoryName + " exists"); | |
File file = new File(directoryName.concat("\\" + fileName)); | |
if(file.exists() && !file.isDirectory()) { | |
FileClient.setFile(file); | |
return; | |
} | |
File directory = new File(directoryName); | |
String[] subDirectories = directory.list(new FilenameFilter() { | |
@Override | |
public boolean accept(File current, String name) { | |
return new File(current, name).isDirectory(); | |
} | |
}); | |
if (subDirectories.length > 0) { | |
System.out.println("subdirectories for " + directoryName + ": " + Arrays.toString(subDirectories)); | |
String[] subDirectoriesNames = new String[subDirectories.length]; | |
for (int i = 0; i < subDirectoriesNames.length; i++) { | |
subDirectoriesNames[i] = directoryName.concat("\\" + subDirectories[i]); | |
} | |
ArrayList<Thread> subDirectoriesThreads = new ArrayList<>(subDirectoriesNames.length); | |
for (String subDirectoryName : subDirectoriesNames) { | |
Thread thread = new Thread(new FileFounder(fileName, subDirectoryName)); | |
subDirectoriesThreads.add(thread); | |
thread.start(); | |
} | |
try { | |
for (Thread thread : subDirectoriesThreads) | |
thread.join(); | |
} catch (InterruptedException exception) { | |
exception.printStackTrace(); | |
} | |
} else { | |
System.out.println("there's no subdirectories in the directory " + directoryName); | |
} | |
} else { | |
System.out.println("directory " + directoryName + " doesn't exist"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment