main.java

来自「java的经典例子」· Java 代码 · 共 42 行

JAVA
42
字号
import java.io.*;

class Main {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Main <inputfile>");
            System.exit(-1);
        }
        try {
            File f = new File(args[0]);
            LineNumberReader in = new LineNumberReader(new FileReader(f));

            int charcount = (int)f.length();
            in.mark(charcount); // mark at beginning of stream
            in.skip(charcount); // skip to end of buffer
            int linecount = in.getLineNumber();

            System.out.println("file:\t" + args[0] +
                               "\nlines:\t" + linecount + 
                               "\nchars:\t" + charcount +
                               "\ncontent:");
            in.reset();        
            char[] buf = new char[100];
            int howmany = in.read(buf);

            // Echo characaters
            for (int i = 0; i < howmany; i++)
                System.out.write(buf[i]);
            System.out.println();

            // Indicate whether there are more
            if (charcount >= 100) 
                System.out.println("...");
            System.out.flush();         // flush output

            in.close();                 // closes all "downstream" readers
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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