Skip to content

Commit

Permalink
Homework5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hnoianko committed Dec 4, 2017
1 parent e812f59 commit 23c1f34
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Homework5.2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Homework5.2.iml
.idea/
out/
1 change: 1 addition & 0 deletions Homework5.2/One.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My name is Bohdan
1 change: 1 addition & 0 deletions Homework5.2/Result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My name is
1 change: 1 addition & 0 deletions Homework5.2/Two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My name is Vlad
64 changes: 64 additions & 0 deletions Homework5.2/src/com/gmail/gnoianko/FileWork.java
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);
}
}
14 changes: 14 additions & 0 deletions Homework5.2/src/com/gmail/gnoianko/Main.java
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);
}


}

0 comments on commit 23c1f34

Please sign in to comment.