Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static String[] readFromFile() throws IOException {
- Scanner in = new Scanner(System.in);
- String inputFileName;
- System.out.println("Enter a file name: ");
- inputFileName = in.nextLine();
- inputFileName = inputFileName + ".txt";
- FileInputStream inFile = new FileInputStream(inputFileName);
- byte[] line = new byte[inFile.available()];
- inFile.read(line);
- String text = new String(line);
- String[] words = text.split(" ");
- return words;
- }
- public static void largestWord_s(String[] words) {
- String[] mas_words = words.split(" ");
- String max = mas_words[0];
- for (int i = 1; i < mas_words.length; ++i) {
- if (max.length() < mas_words[i].length()) {
- max = mas_words[i];
- }
- }
- System.out.println("The longest word is :" + max);
- }
- public static void writeToFile(String largestWord) throws IOException {
- Scanner in = new Scanner(System.in);
- int i;
- String newFileName;
- System.out.println("Enter file name for save:");
- newFileName = in.nextLine();
- newFileName = newFileName + ".txt";
- FileWriter fileWriter = new FileWriter(new File(newFileName));
- fileWriter.write("The longest word is:" + largestWord);
- fileWriter.flush();
- }
- public static void main(String[] args) throws IOException {
- String[] words = new String[0];
- words = readFromFile();
- System.out.println(words);
- String longWord = largestWord_s(words);
- writeToFile(longWord);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment