当前位置: 代码迷 >> Java相关 >> nullpointerexception问题求助!!!
  详细解决方案

nullpointerexception问题求助!!!

热度:659   发布时间:2011-12-20 22:06:34.0
nullpointerexception问题求助!!!
import java.io.*;
public class FileOperation{
public static void main(String args[]){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
File myfile=new File("Line.txt");
String s;
try{
BufferedWriter bw=new BufferedWriter(new FileWriter(myfile));
while(true){  
  System.out.println("输入字符串:");
  s=br.readLine();
  if(s.length()==0) break;
  bw.write(s);
  bw.newLine();
  }
  bw.close();
  }
  catch(IOException e){}  
  try{
  BufferedReader brF=new BufferedReader(new FileReader(myfile));
  while(true){
  s=brF.readLine();
  if(s.length()==0) break;
  }  }
  catch(IOException e){}                                                                     
}
}
求教大神们此处该如何改,谢谢呀!!
搜索更多相关的解决方案: class  public  import  字符串  

----------------解决方案--------------------------------------------------------
程序代码:
import java.io.*;
public class FileOperation{
public static void main(String args[]){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
File myfile=new File("Line.txt");
String s;
try{
BufferedWriter bw=new BufferedWriter(new FileWriter(myfile));
while(true){  
  System.out.println("输入字符串:");
  s=br.readLine();
  if(s.length()==0) break;
  bw.write(s);
  bw.newLine();
  }
  bw.close();
  }
  catch(IOException e){}  
  try{
  BufferedReader brF=new BufferedReader(new FileReader(myfile));
  while(true){
  s=brF.readLine();
  if(s==null) break;
  System.out.println(s);
  }
  brF.close();
  }
  catch(IOException e){}                                                                     
}
}

主要是读文件时文件结束判断错误,应该是 if(s==null) break;
----------------解决方案--------------------------------------------------------
回复 2楼 lucky563591
非常感谢,按你的方法确实对的,可是我还是不明白为什么用if(s.length()==0)来判断结束就不行?你能讲讲吗?
----------------解决方案--------------------------------------------------------
怎么没人回了哦?人气不旺呀
----------------解决方案--------------------------------------------------------
路过
----------------解决方案--------------------------------------------------------
  相关解决方案