2014-10-29 24 views
0

我的文件中包含的数据是Treetagger如何打印文件的第三列在Java

big JJ big 

    nice JJ nice 

    full JJ full 

    big JJ big 

    best JJS good 

的输出这里是我的代码

File f = new File(n[x]); 
    BufferedReader br = new BufferedReader(new FileReader(f)); 
    String s; 
    String filedata = " "; 
    while ((s = br.readLine()) != null) { 
     filedata += s + " "; 
    } 

    StringTokenizer stk = new StringTokenizer(filedata, " "); 
    while (stk.hasMoreTokens()) { 
     String token = stk.nextToken("JJ"); 
     String[] to = token.split(" ", 2); 
     String ft = to[1]; 
     a.write(ft); 
    } 

第三列是朵朵的话。如何打印?

+0

'字符串[]为=令牌。 split(“”,2);'你将分割数量限制为2,你应该更新为3或者将第二个参数保留。之后,您可以使用[2] – Erwin 2014-10-29 08:57:42

+2

获取第三个值。请勿使用StringTokenizer。它已被弃用。你可以使用分割字符串。 – Tarek 2014-10-29 08:58:43

+0

可能是你正在使用的分隔符是追加字符串实际上是不推荐我猜可能是使用'|'然后拆分字符串作为'|'delimeter – Babel 2014-10-29 09:15:55

回答

1

下面的代码:

String[] splitdata = filedata.split(" "); 

String thirdcol = splitdata[2]; 

System.out.println(thirdcol); 

希望它会帮助你。

+0

不是'splitdata [2] *第三*列? – 2014-10-29 09:09:04

+0

不,它不是第三列 – user3387277 2014-10-29 09:29:31

+0

splitdata [0]是第一列,但[3]不在b界 – user3387277 2014-10-29 09:37:25

0

String.split方法可用于将一个字符串分解成它的基本标记,并采取第三之一:

String[] result = filedata.split(" "); 
String col3 = result[2]; 
0

希望这将有助于

File f=new File(n[x]); 
    BufferedReader br = new BufferedReader(new FileReader(f)); 
    String s; 
    while((s=br.readLine())!=null){ 

String test = s.replace(" ",""); 

String[] test2 = test.split("JJ"); 

String test3 = test2[1]; 

System.out.println(test3); 
} 
相关问题