exercise16_1.java

来自「一个基于JAVA的简单的GUI编程」· Java 代码 · 共 50 行

JAVA
50
字号
import java.io.*;
public class Exercise16_1 {
  public static void main(String args[]) {
    FileReader in = null;
    FileWriter out = null;
    int charCount = 0, wordCount = 0, lineCount = 0;
    boolean newWord = true, newLine = false;
    if (args.length != 1) {
      System.out.println("Usage: java Exercise16_1 file");
      System.exit(0);
    }
    try {
      in = new FileReader(new File(args[0]));
      int r;
      while ((r = in.read())!= -1) {
        charCount++;
        if ((char)r == '\n') {
          lineCount++;
          newLine = true;
        }
        else
          newLine = false;

        if (((r == ' ') || (r == '\n') || (r == '\t')) && !newWord)
          newWord = true;
        if ((r != ' ') && (r != '\n') && (r!= '\t') && newWord) {
          newWord = false;
          wordCount++;
        }
      }
      if (!newLine) lineCount++;
      System.out.println("File " + args[0] + " has \n" + charCount +
        " characters, \n" + wordCount + " words, and \n" + lineCount
        + " lines.");
    }
    catch (FileNotFoundException ex) {
      System.out.println("File not found: " + args[0]);
    }
    catch (IOException ex) {
      System.out.println(ex.getMessage());
    }
    finally {
      try {
        if (in != null) in.close();
        if (out != null) out.close();
      }
      catch (IOException ex) {}
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?