代码如下:
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("hd1.txt")));
line = reader.readLine();
System.out.println(line);
if(line == null)
return "文件为空!";
//else {
if(line != null){
StringTokenizer stringTokenizer = new StringTokenizer(line,"\u5639\u563b");
while(stringTokenizer.hasMoreTokens()){
String phone = stringTokenizer.nextToken();
String[] phoneInfo = phone.split(",");
String tempNumber = phoneInfo[1].substring(1,phoneInfo[1].length()-1);
//System.out.println(phone);
if(tempNumber.equals(phoneNumber.substring(0,7))){
cityName = phoneInfo[0].substring(1,phoneInfo[0].length()-1);
break;
}
}
}
reader.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}hd1.txt文件:
"col001","col002","mobileflag","id"
"阿坝","1388248","qqt",1
"阿坝","1388249","qqt",2
"阿坝","1399040","qqt",3
"阿坝","1399041","qqt",4
"阿坝","1399042","qqt",5
为社么读取到hd1.txt文件的第一行?
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("hd1.txt")));
line = reader.readLine();
System.out.println(line);
if(line == null)
return "文件为空!";
//else {
if(line != null){
StringTokenizer stringTokenizer = new StringTokenizer(line,"\u5639\u563b");
while(stringTokenizer.hasMoreTokens()){
String phone = stringTokenizer.nextToken();
String[] phoneInfo = phone.split(",");
String tempNumber = phoneInfo[1].substring(1,phoneInfo[1].length()-1);
//System.out.println(phone);
if(tempNumber.equals(phoneNumber.substring(0,7))){
cityName = phoneInfo[0].substring(1,phoneInfo[0].length()-1);
break;
}
}
}
reader.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}hd1.txt文件:
"col001","col002","mobileflag","id"
"阿坝","1388248","qqt",1
"阿坝","1388249","qqt",2
"阿坝","1399040","qqt",3
"阿坝","1399041","qqt",4
"阿坝","1399042","qqt",5
为社么读取到hd1.txt文件的第一行?
line = reader.readLine();应该像这样循环读
while ((line = reader.readLine()) != null)
{
....
}
才能循环读取所有行
{
....
}
正解
改为while(line!=null)
才能循环读取所有行
line = reader.readLine();应该像这样循环读
while ((line = reader.readLine()) != null)
{
....
}
{
....
}
正解
* Read a line of text. A line is considered to be terminated by any one
* of a line feed ('\n'), a carriage return ('\r'), or a carriage return
* followed immediately by a linefeed.
*
* @return A String containing the contents of the line, not including
* any line-termination characters, or null if the end of the
* stream has been reached
*
* @exception IOException If an I/O error occurs
*/