The program finds a long word in the file, tell me how it can be deleted?

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class WordFinder { public static void main(String [ ] args) throws FileNotFoundException { new WordFinder().getLongestWord(); } public String getLongestWord() throws FileNotFoundException { String longestWord = ""; String current; Scanner scan = new Scanner(new File("file.txt")); while (scan.hasNext()) { current = scan.next(); if (current.length() > longestWord.length()) { longestWord = current; } } System.out.println(longestWord); return longestWord; } } 
  • one
    what about creating a new file and writing down everything you need? - michael_best

1 answer 1

Here is my solution. For ease of understanding, I have broken my uniform code into methods.

In the findLongestString method, we take the file name, scan it, and by comparing the length of strings ( if (longest.length() < currentWord.length()) ) we find the longest word. If there are 2 or more such long words, the program returns the value of only the first big word that came along the path. After returning the return longest value, we assign the received word to the longest variable in the main method to use it in another method (could do it through a global variable instead of a local one, but it would not be interesting :))

In the deleteWord method deleteWord I take the resulting long word and file name. Next, I create a file read and file write (because in Java I, personally, do not know another way to replace / delete lines). Why do I need these two things? I read a file in which there is a long word and if the line is not that word, I write the data to a temporary file temp.txt , if it is - I skip writing this line to the file with continue . As a result, after reading the file, I received 2 files - temp.txt and test2.txt (as in my example). The temp,txt file temp,txt is the newly received file after deleting the line, and test2.txt is the old one. What needs to be done next?) Correctly, replace one file with another. We do this through tempFile.renameTo(inputFile); .

In the printFile method printFile I simply display the file. Because I recorded this function at the end - the program will display the test2.txt file with the long word removed.

 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class WordFinder { public static void main(String[] args) { String filename = "test2.txt"; WordFinder wordFinder = new WordFinder(); String longest = wordFinder.findLongestString(filename); wordFinder.deleteWord(longest, filename); wordFinder.printFile(filename); } public String findLongestString(String filename) { String longest = ""; try { Scanner scanner = new Scanner(new File(filename)); while(scanner.hasNextLine()) { String currentWord = scanner.nextLine(); if (longest.length() < currentWord.length()) { longest = currentWord; } } scanner.close(); } catch (IOException e) { e.printStackTrace(); } return longest; } public void deleteWord(String longestWord, String filename) { try { File inputFile = new File(filename); File tempFile = new File("temp.txt"); BufferedReader reader = new BufferedReader(new FileReader(inputFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); String currentLine; while((currentLine = reader.readLine()) != null) { if(currentLine.equals(longestWord)) continue; writer.write(currentLine+"\n"); } writer.close(); reader.close(); tempFile.renameTo(inputFile); } catch (IOException e) { e.printStackTrace(); } } public void printFile(String filename) { try { Scanner scanner = new Scanner(new File(filename)); while(scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } scanner.close(); } catch (IOException e) { e.printStackTrace(); } } }