forked from Hnoianko/prog.kiev.ua-JavaOOP-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Homework5.2.iml | ||
.idea/ | ||
out/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
My name is Bohdan |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
My name is |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
My name is Vlad |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.gmail.gnoianko; | ||
|
||
import java.io.*; | ||
|
||
public class FileWork { | ||
public static String loadTextFromFile(File file) { | ||
if (file == null) { | ||
throw new IllegalArgumentException("Null file pointer"); | ||
} | ||
StringBuilder sb = new StringBuilder(); | ||
try (BufferedReader br = new BufferedReader(new FileReader(file))) { | ||
String text = ""; | ||
for (; (text = br.readLine()) != null;) { | ||
sb.append(text); | ||
sb.append(System.lineSeparator()); | ||
} | ||
} catch (IOException e) { | ||
System.out.println(e); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public static String[] getWords(String textline) { | ||
String[] words = textline.split("[ .,\n!]"); | ||
return words; | ||
} | ||
|
||
public static boolean isWordsInText(String word, String text) { | ||
String[] textArray = getWords(text); | ||
for (String wordOne : textArray) { | ||
if (word.equals(wordOne)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static void saveTextToFile(String text, File file) { | ||
if (file == null || text == null) { | ||
throw new IllegalArgumentException("Null pointer"); | ||
} | ||
try (PrintWriter pw = new PrintWriter(file)) { | ||
pw.println(text); | ||
} catch (IOException e) { | ||
System.out.println(e); | ||
} | ||
} | ||
|
||
public static void saveEqualsWords(File one, File two, File result) { | ||
if (one == null || two == null || result == null) { | ||
throw new IllegalArgumentException("null pointer"); | ||
} | ||
String textOne = loadTextFromFile(one); | ||
String textTwo = loadTextFromFile(two); | ||
StringBuilder sb = new StringBuilder(); | ||
String[] words = getWords(textOne); | ||
for (String word : words) { | ||
if (isWordsInText(word, textTwo)) { | ||
sb.append(word + " "); | ||
} | ||
} | ||
saveTextToFile(sb.toString(), result); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.gmail.gnoianko; | ||
|
||
import java.io.File; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
File fileOne = new File("E:\\КУРСИ JAVA\\javaOOP\\IdeaProjectsOOP\\Homework5.2\\One.txt"); | ||
File fileTwo = new File("E:\\КУРСИ JAVA\\javaOOP\\IdeaProjectsOOP\\Homework5.2\\Two.txt"); | ||
File fileResult = new File("E:\\КУРСИ JAVA\\javaOOP\\IdeaProjectsOOP\\Homework5.2\\Result.txt"); | ||
FileWork.saveEqualsWords(fileOne, fileTwo, fileResult); | ||
} | ||
|
||
|
||
} |