1
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Example {

public static void main(String[] args) throws IOException 
{
File fis=new File("D:/Testcode/Test.txt");
BufferedReader br;
String input;
String var = null;
if(fis.isAbsolute())
{
br=new BufferedReader(new FileReader(fis.getAbsolutePath()));
while ((input=br.readLine())!=null) {
var=input;
}
}   
//String var="Duminy to Warner, OUT, Duminy gets a wicket again. He has been breaking...
if(var!=null)
{
String splitstr[]=var.split(",");
if(splitstr[0].contains("to"))
{
String ss=splitstr[0];
String a[]=ss.split("\\s+");
int value=splitstr[0].indexOf("to");
System.out.println("Subject:"+splitstr[0].substring(0,value));
System.out.println("Object:"+splitstr[0].substring(value+2));
System.out.println("Event:"+splitstr[1]);
int count=var.indexOf(splitstr[2]);
System.out.println("Narrated Information:"+var.substring(count));
}   
}
}
}

上述程序显示以下输出: 主题:Duminy 对象:Warner 事件:OUT 叙述信息:Duminy 再次获得检票口。他已经断了……

我的问题是,文本可能包含示例“Dumto to Warner, OUT, Duminy got a wicket again. He has been broken...” 意思是,上面的程序不会像上面那样显示输出..如何识别文本之后检查条件的空间

4

2 回答 2

1

您可以这样阅读,但不鼓励这样做。小心。

    File read=new File("D:\\Test.txt");
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(read),Charset.forName("UTF-8")));
    String news = reader.readLine();
    news = news.replace(",", "");
    String[] names = news.split(" ", 6);

    System.out.println("First String : "+names[0]+" "+names[1]);
    System.out.println("Second String : "+names[3]);
    System.out.println("Third String : "+names[4]);
    System.out.println("Fourth String : "+names[5]);
于 2014-03-03T11:46:45.603 回答
0
  • 您必须找到“ to”的第一个发生。一切都在它之前the first string。毕竟它是我们继续使用的字符串:
  • 您必须找到“,”的第一次和第二次出现,从而将剩余的字符串分成三个部分:在第一个“,”之前,在“,”之间,以及在第二个“,”之后。
  • 第一部分变为the second string.
  • 第二部分变为the third string
  • 第三部分变成了the fourth string
于 2014-03-03T11:32:01.837 回答