为什么客户端能接受到反馈回来的用户名密码但是配置文件为空而且后续功能不能使用呢?
代码如下 可能注释的不是太清楚 望指教
//多用户登录服务端
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
public class 多用户登录服务端 extends Thread {
Socket socket;
static File file=new File("E://login.Properties");
static {
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
ServerSocket sersocket=new ServerSocket(9000);
while (true)
//不同的用户获取各自的socket
{ Socket socket= sersocket.accept();
new 多用户登录服务端(socket).start();
}
}
public 多用户登录服务端(Socket socket) {
// TODO 自动生成的构造函数存根
this.socket=socket;
}
public void run()
{while(true)
try{
//获取socket的输入流对象
{ BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获取socket的输出流对象
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(socket.getOutputStream());
String user=bufferedReader.readLine();
//将选项 姓名 密码分开并用数组存储
String[] data=user.split(" ");
String option=data[0];
String username=data[1];
String password=data[2];
if("a".equalsIgnoreCase(option))
//登录
{ Properties properties=new Properties();
properties.load(new FileReader(file));
if(properties.containsKey(username))
{String pass=properties.getProperty(username);
if(pass.equals(password))
{
outputStreamWriter.write("欢迎"+username+"登录\r\n");
}
else{
outputStreamWriter.write("密码错误\r\n");
}
}else
{ outputStreamWriter.write("用户名不存在\r\n");}
outputStreamWriter.flush();
}
else if("b".equalsIgnoreCase(option))
//注册
{ Properties properties=new Properties();
properties.load(new FileReader(file));
if(!properties.containsKey(username))
{ properties.setProperty(username, password);
properties.store(new FileWriter(file), "user\r\n");
outputStreamWriter.write("注册成功");
}
else{
outputStreamWriter.write("用户名已存在,请重新注册\r\n");
}
}
outputStreamWriter.flush();
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//多用户登录客户端
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class 多用户登录客户端 {
public static void main(String[] args) throws IOException {
Socket socket=new Socket(InetAddress.getLocalHost(),9000);
//获取socket的输出流对象
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(socket.getOutputStream());
//获取socket的输入流对象
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//从键盘接收选项
BufferedReader keyReader=new BufferedReader( new InputStreamReader(System .in));
while (true){
System.out.println("请选择 登录(A) 注册(B)");
String option=keyReader.readLine();
if("a".equalsIgnoreCase(option)){
login(outputStreamWriter,keyReader,option);
//读取服务端的反馈信息
String line=bufferedReader.readLine();
System.out.println(line);
}else if("b".equalsIgnoreCase(option))
{
login(outputStreamWriter,keyReader,option);
//读取服务端的反馈信息
String line=bufferedReader.readLine();
System.out.println(line);
}
}
}
//获取用户名密码并传给服务端
public static void login(OutputStreamWriter outputStreamWriter,BufferedReader keyreader, String option) throws IOException
{
System.out.println("请输入用户名");
String username= keyreader.readLine();
System.out.println("请输入密码");
String password=keyreader.readLine();
String line=option+" "+username+" "+keyreader+"/r/n";
outputStreamWriter.write(line);
outputStreamWriter.flush();
}
}