日期:2014-05-20  浏览次数:20738 次

提取文本指定内容(在线等,急哈!)
如果下面是一个“txt”文档


Titanic



Theatrical poster


Directed by
James Cameron

Produced by
James Cameron
Jon Landau





Written by
James Cameron


Starring



Leonardo DiCaprio
Kate Winslet





Music by
James Horner


Cinematography
Russell Carpenter


Editing by



Conrad Buff
James Cameron
Richard A. Harris





Studio



20th Century Fox[1]
Paramount Pictures[1]
Lightstorm Entertainment[1]





Distributed by



Paramount Pictures (USA)
20th Century Fox (International)





Release date(s)



November 1, 1997 (1997-11-01) (Tokyo International Film Festival)
December 19, 1997 (1997-12-19) (United States)





Running time
194 minutes


Country
United States


Language
English


Budget
$200 million[2][3][4]


我要提取目录下面的信息(比如Directed by下面的James Cameron赋值给一个String变量,提取整个txt文档的这些目录下的详细内容),请问如何实现

------解决方案--------------------
你只要比较几个关键词就行了

Java code

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

public class ReadFile 
{
    private static final String[] types = new String[]     //存储关键字
        {
            "Directed by",
            "Produced by",
            "Written by",
            "Starring",
            "Music by",
            "Cinematography",
            "Editing by",
            "Studio",
            "Distributed by",
            "Release date(s)",
            "Running time",
            "Country",
            "Language",
            "Budget"
        };
    private ArrayList<FileStruct> fileStructs = new ArrayList<FileStruct>();    //存储数据
    
    public ReadFile(String filename) throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader(filename));
        
        String str = null;
        while ((str = in.readLine()) != null)
        {
            if (isType(str))    //是关键字的话,就添加新的FileStruct
            {
                fileStructs.add(new FileStruct(str));
            }
            else if (! str.equals("") && fileStructs.size() != 0)
            {
                FileStruct fileStruct = fileStructs.get(fileStructs.size() - 1);
                fileStruct.add(str);    //在最后的FileStruct中添加信息
            }
        }
        
        in.close();
    }
    
    /**
     * 打印信息
     */
    public void printInfos()
    {
        for (FileStruct fileStruct : fileStructs)
        {
            System.out.println(fileStruct.type);
            for (int i = 0; i < fileStruct.list.size(); i++)
            {
                System.out.println("(" + (i + 1) + ") " + fileStruct.list.get(i));
            }
            System.out.println();
        }
    }
    
    /**
     * 判断是否是关键字
     * @param str
     * @return
     */
    private boolean isType(String str)
    {
        for (int i = 0 ; i< types.length; i++)
        {
            if (types[i].equals(str))
            {
                return true;
            }
        }
        return false;
    }
    
    public static void main(String[] args) throws IOException
    {
        ReadFile readFile = new ReadFile("1.txt");
        readFile.printInfos();
    }
}

class FileStruct
{
    public String type;
    public ArrayList<String> list;
    
    public FileStruct(String type)
    {
        this.type = type;
        list = new ArrayList<String>();
    }
    
    public void add(String str)
    {
        list.add(str);
    }
}