2013-10-31 50 views
-2

我的输入文件......如何在java中的分隔符后跳过每一行?

Amrozi accused his brother, whom he called "the witness", of deliberately distorting his evidence. 
agt(accuse(icl>do,equ>charge,cob>abstract_thing,agt>person,obj>person)[email protected]@past,amrozi) 
pos(brother(icl>male_sibling>thing,ant>sister),he(icl>person):01) 
obj(accuse(icl>do,equ>charge,cob>abstract_thing,agt>person,obj>person)[email protected]@past,brother(icl>male_sibling>thing,ant>sister)) 
obj(call(icl>do,com>communicate,plt>thing,plf>thing,agt>person,obj>volitional_thing)[email protected],brother(icl>male_sibling>thing,ant>sister)) 
agt(call(icl>do,com>communicate,plt>thing,plf>thing,agt>person,obj>volitional_thing)[email protected],he(icl>person):02) 
nam(brother(icl>male_sibling>thing,ant>sister),witness(icl>perceiver>thing)[email protected]@double_quote) 
man:01(distort(icl>falsify>do,agt>person,obj>thing)[email protected],deliberately(icl>how,equ>intentionally,ant>accidentally,com>deliberate)) 
pos:01(evidence(icl>indication>thing),he(icl>person):03) 
obj:01(distort(icl>falsify>do,agt>person,obj>thing)[email protected],evidence(icl>indication>thing)) 
obj(brother(icl>male_sibling>thing,ant>sister),:01) 
### 
Referring to him as only "the witness", Amrozi accused his brother of deliberately distorting his evidence. 
cob:01(refer(icl>consider>be,cob>uw,obj>thing)[email protected],he(icl>person):01) 
man:01(only(icl>how,equ>merely),as(icl>how,equ>equally,com>equal)) 
mod:01(witness(icl>perceiver>thing)[email protected]@double_quote,only(icl>how,equ>merely)) 
obj:01(refer(icl>consider>be,cob>uw,obj>thing)[email protected],witness(icl>perceiver>thing)[email protected]@double_quote) 
agt(accuse(icl>do,equ>charge,cob>abstract_thing,agt>person,obj>person)[email protected]@past,amrozi) 
pos(brother(icl>male_sibling>thing,ant>sister),he(icl>person):02) 
obj(accuse(icl>do,equ>charge,cob>abstract_thing,agt>person,obj>person)[email protected]@past,brother(icl>male_sibling>thing,ant>sister)) 
man:02(distort(icl>falsify>do,agt>person,obj>thing)[email protected],deliberately(icl>how,equ>intentionally,ant>accidentally,com>deliberate)) 
pos:02(evidence(icl>indication>thing),he(icl>person):03) 
obj:02(distort(icl>falsify>do,agt>person,obj>thing)[email protected],evidence(icl>indication>thing)) 
man(accuse(icl>do,equ>charge,cob>abstract_thing,agt>person,obj>person)[email protected]@past,:01) 
obj(brother(icl>male_sibling>thing,ant>sister),:02) 
### 
Yucaipa owned Dominick's before selling the chain to Safeway in 1998 for $2.5 billion. 
nam([email protected],dominick) 
obj(own(icl>be,equ>posess,obj>thing,aoj>thing)[email protected],dominick) 
tim([email protected],before(icl>how,tim<uw,obj>thing)) 
obj:01(sell(icl>exchange>do,cob>thing,agt>thing,obj>thing,ptn>thing)[email protected],chain(icl>series>thing)[email protected]) 
ptn:01(sell(icl>exchange>do,cob>thing,agt>thing,obj>thing,ptn>thing)[email protected],safeway) 
tim:01(safeway,1998) 
cob:01(sell(icl>exchange>do,cob>thing,agt>thing,obj>thing,ptn>thing)[email protected],_) 
mod:01(_,"2.5") 
mod:01("2.5",billion(icl>quantity)) 
obj(before(icl>how,tim<uw,obj>thing),:01) 
### 

我需要阅读文件的相关内容,并通过跳过第一线和###后每行到一个新的文件,将其写入到另一个文件。我需要与###一起的内容写入新file..only我需要的###后删除线。我做了下面的代码,但它不能正常工作...

Scanner scanner1 = new Scanner(new File("E:out1.txt")); 
scanner1.useDelimiter("###"); 
BufferedWriter fos1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:out2.txt"))); 
String line1=""; 
//while ((line1 = br.readLine()) != null)      
while(scanner1.hasNextLine())//&&scanner1.hasNext()) { 
    // scanner1.nextLine(); 
    if(scanner1.nextLine().equals("###")) { 
     scanner1.nextLine(); 
    } 
    fos1.append(scanner1.nextLine()); 
    fos1.newLine(); 

请帮忙...

+3

'我做了下面的代码,但它不能正常工作...'它在干什么? – Cruncher

回答

1

每当您拨打nextLine,您需要我是一条线。

编辑答案,认识到你想要的“###”线,但没有行之后

试试这个作为你的while循环:

while(scanner1.hasNextLine()) 
{ 
    String line = scanner1.nextLine(); 
    fos1.append(line); 
    fos1.newLine(); 
    if(line.equals("###")) 
    { 
     scanner1.nextLine(); 
    } 
} 

现在我们不消耗“ ###“当我们测试时,如果这是线。

+0

没有它的不working.it将空值打印到文件 – user2942551

+0

@ user2942551我的意思是使这个while循环的整个身体。如果你在这之前读过一行,那么你可以编辑空指针 – Cruncher

+0

@ user2942551,使其更清楚你最终的结果应该看起来像 – Cruncher