如何在单独的方法中读取和处理文件并在 Java 的主要方法中调用它?

How do I read and process a file in a separate method and call it in the main method in Java?

我正在处理这段代码,用于计算从 .txt 文件中读取的给定文本的 ARI。这段代码工作得很好,但我想在我的主要方法中加入更少的东西。它如何将我的 try 和 catch 块放在一个新方法中,然后将它调用到我的 main 方法,而不是将所有内容混合在一起?我已经尝试了一些方法,但我没有得到通常的输出。

这是我的主要内容 class:

public class MyClass {
public static void main(String[] args){
    String EachSentence;
    int wordCount;

    List<Sentence> sentences = new ArrayList<>();
    List<Integer> EachWordCount = new ArrayList<>();
    List<Integer> EachLetterCounted= new ArrayList<>();

    try{
        File file = new File("a2b.txt");
        Scanner scanner = new Scanner(file);
        scanner.useDelimiter("[.?!]");
        int SentenceCount =0;
        while(scanner.hasNext()){
            SentenceCount++;
            EachSentence = scanner.next();
            EachSentence = EachSentence.replaceAll("\r?\n", " ");
            EachSentence = EachSentence.trim();
            if (sentences.add(new Sentence(EachSentence)) && sentences.size() > 1){
                System.out.println("(" + SentenceCount + ") " + EachSentence);
            }
            StringTokenizer tokenizer = new StringTokenizer(EachSentence, " ");
            wordCount = tokenizer.countTokens();
            if(EachWordCount.add(wordCount)&& EachWordCount.size() > 1){
                //I removed the element at position 0 so as to correlate with EachSentence then added the counts to the arrayList
                //to prepare them for addition
                EachWordCount.remove(0);
                EachWordCount.add(wordCount);
            }
            int LetterCount=1;
            for(int i=1; i<EachSentence.length(); i++){
                char currentChar = EachSentence.charAt(i);
                if(currentChar != ' '&& currentChar!='('&&currentChar!=')'&&currentChar!=','&& currentChar!='.'){
                    EachLetterCounted.add(LetterCount);
                    LetterCount++;
                }
            }
        }
        scanner.close();
    }catch (IOException ex){ 
        System.out.println(ex.getMessage());
    }


    //Computes the ARI of the total sentences
    double lettersCounted = (double) lettersCounted(EachLetterCounted);
    double wordsCounted = (double) sum(EachWordCount);
    double sentencesCounted = (double) SentencesCounted(EachWordCount);
    double AutomatedReadabilityIndex= 4.71*(lettersCounted/wordsCounted) + 0.5 * (wordsCounted/sentencesCounted) -21.43;

    System.out.println("\nSummary statistics: ");
    System.out.println("Letters: "+lettersCounted(EachLetterCounted));

    System.out.println("Words: "+ sum(EachWordCount));
    System.out.println("Sentences: " + SentencesCounted(EachWordCount));

    DecimalFormat df = new DecimalFormat("#.##");
    System.out.println("readability: "+ df.format(AutomatedReadabilityIndex));
}

贡献会很棒!

第二次回答我的问题。(对于遇到同样问题的任何人)我通过简单地将我在 main 方法中拥有的所有内容放在 ...static void ReadFile() 方法中并仅在 main 方法中调用 `ReadFile 来实现这一点。这可能不是最好的方法,但这是我一直在寻找的方法:)

public static void main(String[] args) {
    ReadFile();
}

public static void ReadFile() {
   //insert all what is in main method, the exceptions were handled
   //here also. So there was no need for the throws IOException that 
   //should've followed.
}

public static void main(String[] args) {
    ReadFile(//insert name of file to be read here);
}

public static void ReadFile(String filename) {
   //insert all what is in main method, the exceptions were handled
   //here also. So there was no need for the throws IOException that 
   //should've followed.
}