BufferedReader使用readLine()方法返回一个奇葩的null值,麻烦指教

小K不小 2014-12-20 07:08:03

如图,打印这个值是一个null值,比较却false
import java.net.*;
import java.io.*;
//客户端
class LoginClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.100",8888);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
for (int x = 0;x < 3 ;x++ )
{
String line = br.readLine();
pw.println(line);

String str = bufIn.readLine();
System.out.println(str);
if (str.equals("欢迎"))
break;
}
s.close();
}
}
//服务端
class LoginServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while (true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}

}
}
//服务端User线程
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s)
{
this.s = s;
}
public void run()
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"......connect");
try
{

for (int x = 0;x < 3;x++)
{
BufferedReader br = new BufferedReader(new FileReader("123.txt"));
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String str = bufIn.readLine();

System.out.println(str);
System.out.println(str==null);
System.out.println(str=="null");


String line;
boolean flag = false;
while((line=br.readLine())!=null)
{
if(line.equals(str))
{
flag = true;
break;
}
}
if (flag)
{
System.out.println(str+"......上线了");
pw.println("欢迎");
break;
}
else
{
System.out.println(str+".....尝试上线");
pw.println("用户名不存在");
}
}
s.close();
}
catch (Exception e)
{
}
}
}
...全文
971 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyc_ 2014-12-21
  • 打赏
  • 举报
回复
用equals
小K不小 2014-12-21
  • 打赏
  • 举报
回复
引用 1 楼 cumtwyc 的回复:
System.out.println(str);输出null,说明要么是空指针,要么是“null”; System.out.println(str==null);输出false,说明不是空指针,那就只有字符串了 System.out.println(str=="null");输出false,说明“null”是new出来的,而不是直接指向字符串常量。
太感谢了。那我应该怎么判断?能为true
小K不小 2014-12-21
  • 打赏
  • 举报
回复
引用 3 楼 cumtwyc 的回复:
用equals
。。。成功了,昨天用equals可能忘记给null加引号了
wyc_ 2014-12-20
  • 打赏
  • 举报
回复
System.out.println(str);输出null,说明要么是空指针,要么是“null”; System.out.println(str==null);输出false,说明不是空指针,那就只有字符串了 System.out.println(str=="null");输出false,说明“null”是new出来的,而不是直接指向字符串常量。

62,617

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧