ex18(2).java

来自「Thinking In Java 原文章」· Java 代码 · 共 29 行

JAVA
29
字号
// strings/Ex18.java
// TIJ4 Chapter Strings, Exercise 18, page 546
/* Write a program that reads a Java source-code file (you provide the 
* file name on the command line) and displays all the string literals 
* in the code.
*/
// {Args: fileName}
import java.util.regex.*;
import net.mindview.util.*;
import java.io.*;

public class Ex18  {
	public static void main(String[] args) throws Exception {
		if(args.length < 1) {
			System.out.println("Usage: fileName");
			System.exit(0);
		}
		Pattern p = Pattern.compile("\".*\"");
		System.out.println(args[0] + " string literals:");
		// Iterate through the lines of the input file:
		int index = 0;
		Matcher m = p.matcher(""); // creates emtpy Matcher object
		for(String line : new TextFile(args[0])) {
			m.reset(line);
			while(m.find())
				System.out.println(index++ + ": " + m.group());
		}
	}
}

⌨️ 快捷键说明

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