java中读取文件,用readline()读怎么会有的行读不到?
=============程序代码try{BufferedReaderbr=newBufferedReader(newFileReader("C:\\Documentsa...
=============程序代码
try{
BufferedReader br=new BufferedReader(new FileReader("C:\\Documents and Settings\\Administrator\\桌面\\FusionCharts_Evaluation\\Gallery\\Data\\StMSCol2D.xml"));
StringBuffer xmlstr=new StringBuffer();
// System.out.println(br.readLine());
while(br.ready()){
System.out.println(br.readLine());
xmlstr.append(br.readLine());
}
br.close();
//System.out.println(xmlstr.toString());
}catch(Exception e){
}
============== 程序执行结果
<chart palette='3' caption='Break-up of Annual Revenue' subcaption='In Million $' xaxisname='Year' Yaxisname='Sales in M$' numdivlines='3' numberPrefix='$' numberSuffix='M' showSum='1' useRoundEdges='1' legendBorderAlpha='0'>
<category label='2001'/>
<category label='2003'/>
<category label='2005'/>
<dataset>
<set value='30' />
<set value='29' />
<set value='34' />
<dataSet seriesName='Product B' color='F6BD0F' showValues='0'>
<set value='28' />
<set value='41' />
</dataSet>
<dataSet>
<set value='27' />
<set value='28' />
<set value='10' />
<dataset seriesname='Service B' color='A66EDD' showValues='0'>
<set value='15' />
<set value='16' />
</dataset>
<set value='12' />
<set value='16' />
<set value='12' />
</dataSet>
======================文件内容
<chart palette='3' caption='Break-up of Annual Revenue' subcaption='In Million $' xaxisname='Year' Yaxisname='Sales in M$' numdivlines='3' numberPrefix='$' numberSuffix='M' showSum='1' useRoundEdges='1' legendBorderAlpha='0'>
<categories font='Arial' fontSize='12' fontColor='000000'>
<category label='2001'/>
<category label='2002'/>
<category label='2003'/>
<category label='2004'/>
<category label='2005'/>
</categories>
<dataset>
<dataSet seriesName='Product A' color='AFD8F8' showValues='0'>
<set value='30' />
<set value='26' />
<set value='29' />
<set value='31' />
<set value='34' />
</dataSet>
<dataSet seriesName='Product B' color='F6BD0F' showValues='0'>
<set value='21' />
<set value='28' />
<set value='39' />
<set value='41' />
<set value='24' />
</dataSet>
</dataset>
<dataSet>
<dataset seriesname='Service A' color='8BBA00' showValues='0'>
<set value='27' />
<set value='25' />
<set value='28' />
<set value='26' />
<set value='10' />
</dataset>
<dataset seriesname='Service B' color='A66EDD' showValues='0'>
<set value='17' />
<set value='15' />
<set value='18' />
<set value='16' />
<set value='10' />
</dataset>
<dataset seriesname='Service C' color='F984A1' showValues='0'>
<set value='12' />
<set value='17' />
<set value='16' />
<set value='15' />
<set value='12' />
</dataset>
</dataSet>
</chart>
========================
怎么会有的行读不到啊 展开
try{
BufferedReader br=new BufferedReader(new FileReader("C:\\Documents and Settings\\Administrator\\桌面\\FusionCharts_Evaluation\\Gallery\\Data\\StMSCol2D.xml"));
StringBuffer xmlstr=new StringBuffer();
// System.out.println(br.readLine());
while(br.ready()){
System.out.println(br.readLine());
xmlstr.append(br.readLine());
}
br.close();
//System.out.println(xmlstr.toString());
}catch(Exception e){
}
============== 程序执行结果
<chart palette='3' caption='Break-up of Annual Revenue' subcaption='In Million $' xaxisname='Year' Yaxisname='Sales in M$' numdivlines='3' numberPrefix='$' numberSuffix='M' showSum='1' useRoundEdges='1' legendBorderAlpha='0'>
<category label='2001'/>
<category label='2003'/>
<category label='2005'/>
<dataset>
<set value='30' />
<set value='29' />
<set value='34' />
<dataSet seriesName='Product B' color='F6BD0F' showValues='0'>
<set value='28' />
<set value='41' />
</dataSet>
<dataSet>
<set value='27' />
<set value='28' />
<set value='10' />
<dataset seriesname='Service B' color='A66EDD' showValues='0'>
<set value='15' />
<set value='16' />
</dataset>
<set value='12' />
<set value='16' />
<set value='12' />
</dataSet>
======================文件内容
<chart palette='3' caption='Break-up of Annual Revenue' subcaption='In Million $' xaxisname='Year' Yaxisname='Sales in M$' numdivlines='3' numberPrefix='$' numberSuffix='M' showSum='1' useRoundEdges='1' legendBorderAlpha='0'>
<categories font='Arial' fontSize='12' fontColor='000000'>
<category label='2001'/>
<category label='2002'/>
<category label='2003'/>
<category label='2004'/>
<category label='2005'/>
</categories>
<dataset>
<dataSet seriesName='Product A' color='AFD8F8' showValues='0'>
<set value='30' />
<set value='26' />
<set value='29' />
<set value='31' />
<set value='34' />
</dataSet>
<dataSet seriesName='Product B' color='F6BD0F' showValues='0'>
<set value='21' />
<set value='28' />
<set value='39' />
<set value='41' />
<set value='24' />
</dataSet>
</dataset>
<dataSet>
<dataset seriesname='Service A' color='8BBA00' showValues='0'>
<set value='27' />
<set value='25' />
<set value='28' />
<set value='26' />
<set value='10' />
</dataset>
<dataset seriesname='Service B' color='A66EDD' showValues='0'>
<set value='17' />
<set value='15' />
<set value='18' />
<set value='16' />
<set value='10' />
</dataset>
<dataset seriesname='Service C' color='F984A1' showValues='0'>
<set value='12' />
<set value='17' />
<set value='16' />
<set value='15' />
<set value='12' />
</dataset>
</dataSet>
</chart>
========================
怎么会有的行读不到啊 展开
展开全部
while(br.ready()){
System.out.println(br.readLine());
xmlstr.append(br.readLine());
}
每出现一次br.redLine()它就读一行,你这个循环里面写了两次,它就执行了两次
System.out.println(br.readLine());
执行了第一行
xmlstr.append(br.readLine());
执行了第二行
再循环System.out.println(br.readLine());执行的是第三行
你可以这样写
while(br.ready()){
String string = br.readLine();
System.out.println(string);
xmlstr.append(string);
}
System.out.println(br.readLine());
xmlstr.append(br.readLine());
}
每出现一次br.redLine()它就读一行,你这个循环里面写了两次,它就执行了两次
System.out.println(br.readLine());
执行了第一行
xmlstr.append(br.readLine());
执行了第二行
再循环System.out.println(br.readLine());执行的是第三行
你可以这样写
while(br.ready()){
String string = br.readLine();
System.out.println(string);
xmlstr.append(string);
}
展开全部
这段代码有问题:
while(br.ready()){
System.out.println(br.readLine());//这里读取了一行,
xmlstr.append(br.readLine());//这里又读取了一行
}
应改为:
if(br.ready()){
String tmp=null;
while((tmp=br.readLine())!=null){
System.out.println(tmp);
xmlstr.append(tmp);
}
}
while(br.ready()){
System.out.println(br.readLine());//这里读取了一行,
xmlstr.append(br.readLine());//这里又读取了一行
}
应改为:
if(br.ready()){
String tmp=null;
while((tmp=br.readLine())!=null){
System.out.println(tmp);
xmlstr.append(tmp);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
while(br.ready()){
System.out.println(br.readLine());
xmlstr.append(br.readLine());
}
你这个地方用br.readLine()读了两次,但是只把第一次读的内容给显示了出来。
System.out.println(br.readLine());
xmlstr.append(br.readLine());
}
你这个地方用br.readLine()读了两次,但是只把第一次读的内容给显示了出来。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
xmlstr.append(br.readLine());
将此行注释掉就行了
将此行注释掉就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询