创建一个 class 显示文本文件的内容以进行一些更改

Create a class that displays the content of the text file to console with some changes

改动:删除每个单词的前两个字符,长度为4个字符及以上 示例:原始 'qwerty'、新 'erty'

A 'word' 应被视为西里尔字母或拉丁字符的连续序列。

我写了这样的东西,但输出字符串是行的,但我需要输入带有缩进字符的文本。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Part1 {

    // getting input string from the file
    public static String getInput(String fileName) {
        StringBuilder sb = new StringBuilder();
        try {
            Scanner scanner = new Scanner(new File(fileName), "UTF-8");
            while (scanner.hasNextLine()) {
                sb.append(scanner.nextLine()).append(System.lineSeparator());
            }
            scanner.close();
            return sb.toString().trim();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return sb.toString();
    }

    // deleting the first two characters of each word with the length of 4 and more characters
    public static void convert() {
        try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {

            String line;
            StringBuilder result = new StringBuilder();

            while ((line = in.readLine()) != null) {
                String[] wordsInLine = line.split("[.,!?\-\s\n]+");

                for (String string : wordsInLine) {
                    if (isLongerThanFour(string) && defineLocale(string) == "latn") {
                        result.append(string.substring(2) + " ");
                    } else if (isLongerThanFour(string) && defineLocale(string) == "cyrl") {
                        result.append(string.substring(4) + " ");
                    } else {
                        result.append(string + " ");
                    }
                }
            }

            System.out.println(result);
            }catch(IOException e){
                e.getMessage();
            }
        }

    // checking for right length of current word   
    public static boolean isLongerThanFour(String string){
        return string.length() >= 4;
    }

    // define language of input word(one of cyrillic of latin languages)
    private static String defineLocale(String string) {
        char ch = string.charAt(0);

        if (Character.isAlphabetic(ch)) {
            if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
                return "cyrl";
            } else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
                return "latn";
            }
        }
        return "none";
    }

    public static void main(String[] args){
        Part1.convert();
    }

}

你能指出我的错误或提出更清洁的解决方案吗?

提前致谢。

public class Converter {

    //helper enum
    enum Language {
        cyr,
        lat,
        none
    }

    // if you have to return more that two types of values then use enum
    private static Language defineLocale(String string) {
        char ch = string.charAt(0);

        if (Character.isAlphabetic(ch)) {
            if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.CYRILLIC)) {
                return Language.cyr;
            } else if (Character.UnicodeBlock.of(ch).equals(Character.UnicodeBlock.BASIC_LATIN)){
                return Language.lat;
            }
        }

        return Language.none;
    }

    
    public void convert() {
        try (BufferedReader in = new BufferedReader(new FileReader("part1.txt"))) {

            String line;
            StringBuilder result = new StringBuilder();

            while ((line = in.readLine()) != null) {
                String[] wordsInLine = line.split(" ");

                for (String s : wordsInLine) {
                    if (s.length() > 3) {
                        switch (defineLocale(s)) {
                            case cyr:
                                result.append(s.substring(4));
                                break;
                            case lat:
                                result.append(s.substring(2));
                                break;
                            default:
                                result.append(s);
                        }
                    } else result.append(s);
                    result.append(" ");
                }
                
                result.append("\n");//all you were missing
            }

            System.out.println(result);
        }catch(IOException e){
            e.getMessage();
        }
    }
    public static void main(String[] args){
        new Converter().convert();
    }
}

我希望这不需要任何进一步的解释,但如果您不明白,请不要害羞地问。