技术开发 频道

关于Java常用工具您不知道的5件事

  3. Scanner

  有无数 Java 工具能帮助您构建解析器,很多函数语言已成功构建解析器函数库(解析器选择器)。但如果要解析的是逗号分隔值文件,或空格分隔文本文件,又怎么办呢?大多数工具用在此处就过于隆重了,而 String.split() 又不够。(对于正则表达式,请记住一句老话:“ 您有一个问题,用正则表达式解决。那您就有两个问题了。”)

  Java 平台的 Scanner 类会是这些类中您最好的选择。以轻量级文本解析器为目标,Scanner 提供了一个相对简单的 API,用于提取结构化文本,并放入强类型的部分。想象一下,如果您愿意,一组类似 DSL 的命令(源自 Terry Pratchett Discworld 小说)排列在文本文件中,如清单 7:

  清单 7. Igor 的任务

fetch 1 head
fetch
3 eye
fetch
1 foot
attach foot
to head
attach eye
to head
admire

  您,或者是本例中称为 Igor的私仆,能轻松使用 Scanner 解析这组违法命令,如清单 8 所示:

  清单 8. Igor 的任务,由 Scanner 解析

import java.io.*;
import java.util.
*;

public class Igor
    implements IPersonalServant
{
    
public boolean can(String cmd)
    {
        
if (cmd.equals("fetch body parts"))
            return
true;
        
if (cmd.equals("attach body parts"))
            return
true;
        
else
            return
false;
    }
    
public void process(File commandFile)
        throws FileNotFoundException
    {
        Scanner scanner
= new Scanner(commandFile);
        
// Commands come in a verb/number/noun or verb form
        
while (scanner.hasNext())
        {
            
String verb = scanner.next();
            
if (verb.equals("fetch"))
            {
                
int num = scanner.nextInt();
                
String type = scanner.next();
                fetch (num, type);
            }
            
else if (verb.equals("attach"))
            {
                
String item = scanner.next();
                
String to = scanner.next();
                
String target = scanner.next();
                attach(item, target);
            }
            
else if (verb.equals("admire"))
            {
                admire();
            }
            
else
            {
                System.out.println(
"I don't know how to "
                    
+ verb + ", marthter.");
            }
        }
    }
    
    
public void fetch(int number, String type)
    {
        
if (parts.get(type) == null)
        {
            System.out.println(
"Fetching " + number + " "
                
+ type + (number > 1 ? "s" : "") + ", marthter!");
            parts.put(type, number);
        }
        
else
        {
            System.out.println(
"Fetching " + number + " more "
                
+ type + (number > 1 ? "s" : "") + ", marthter!");
            
Integer currentTotal = parts.get(type);
            parts.put(type, currentTotal
+ number);
        }
        System.out.println(
"We now have " + parts.toString());
    }
    
    
public void attach(String item, String target)
    {
        System.out.println(
"Attaching the " + item + " to the " +
            target
+ ", marthter!");
    }
    
    
public void admire()
    {
        System.out.println(
"It'th quite the creathion, marthter");
    }
    
    
private Map<String, Integer> parts = new HashMap<String, Integer>();
}
0
相关文章