【问题标题】:How to solve the readLine() deprecated warning in ChatClient.java?如何解决 ChatClient.java 中的 readLine() 已弃用警告?
【发布时间】:2013-12-12 22:36:08
【问题描述】:

我正在尝试创建一个聊天客户端/服务器网络,我认为它在服务器上工作正常,但客户端有一些问题,因为它有一个不推荐使用的警告,然后程序告诉我使用标志-Xlint 我跑了 javac -Xlint:deprecated ChatClient 但我不知道如何修复这个警告语句:

ChatClient.java:47:警告:DataInputStream 中的 [deprecation] readLine() 已被弃用 streamOut.writeUTF(console.readLine());

这是我的 ChatClient.java 文件

import java.net.*;
import java.io.*;


public class ChatClient implements Runnable
{  
    private Socket socket              = null;
    private Thread thread              = null;
    private DataInputStream  console   = null;
    private DataOutputStream streamOut = null;
    private ChatClientThread client    = null;

public ChatClient(String serverName, int serverPort)
{  
    System.out.println("Establishing connection to server...");

    try
    {
        // Establishes connection with server (name and port)
        socket = new Socket(serverName, serverPort);
        System.out.println("Connected to server: " + socket);
        start();
    }

    catch(UnknownHostException uhe)
    {  
        // Host unkwnown
        System.out.println("Error establishing connection - host unknown: " + uhe.getMessage()); 
    }

    catch(IOException ioexception)
    {  
        // Other error establishing connection
        System.out.println("Error establishing connection - unexpected exception: " + ioexception.getMessage()); 
    }

 }

 public void run()
 {  
    while (thread != null){  
       try
       {  
           // Sends message from console to server
           streamOut.writeUTF(console.readLine());
           streamOut.flush();
       }

       catch(IOException ioexception)
       {  
           System.out.println("Error sending string to server: " + ioexception.getMessage());
           stop();
       }
   }
}


public void handle(String msg)
{  
    // Receives message from server
    if (msg.equals(".quit"))
    {  
        // Leaving, quit command
        System.out.println("Exiting...Please press RETURN to exit ...");
        stop();
    }
    else
        // else, writes message received from server to console
        System.out.println(msg);
}

// Inits new client thread
public void start() throws IOException
{  
    console   = new DataInputStream(System.in);
    streamOut = new DataOutputStream(socket.getOutputStream());
    if (thread == null)
    {  
        client = new ChatClientThread(this, socket);
        thread = new Thread(this);                   
        thread.start();
    }
}

// Stops client thread
public void stop()
{  
    if (thread != null)
    {  
        thread.stop();  
        thread = null;
    }
    try
    {  
        if (console   != null)  console.close();
        if (streamOut != null)  streamOut.close();
        if (socket    != null)  socket.close();
    }

    catch(IOException ioe)
    {  
        System.out.println("Error closing thread..."); }
        client.close();  
        client.stop();
    }


public static void main(String args[])
{  
    ChatClient client = null;
    if (args.length != 2)
        // Displays correct usage syntax on stdout
        System.out.println("Usage: java ChatClient host port");
    else
        // Calls new client
        client = new ChatClient(args[0], Integer.parseInt(args[1]));
}

}

class ChatClientThread extends Thread
{  
    private Socket           socket   = null;
    private ChatClient       client   = null;
    private DataInputStream  streamIn = null;

public ChatClientThread(ChatClient _client, Socket _socket)
{  
    client   = _client;
    socket   = _socket;
    open();  
    start();
}

public void open()
{  
    try
    {  
        streamIn  = new DataInputStream(socket.getInputStream());
    }
    catch(IOException ioe)
    {  
        System.out.println("Error getting input stream: " + ioe);
        client.stop();
    }
}

public void close()
{  
    try
    {  
        if (streamIn != null) streamIn.close();
    }

    catch(IOException ioe)
    {  
        System.out.println("Error closing input stream: " + ioe);
    }
}

public void run()
{  
    while (true)
    {   try
        {  
            client.handle(streamIn.readUTF());
        }
        catch(IOException ioe)
        {  
            System.out.println("Listening error: " + ioe.getMessage());
            client.stop();
        }
    }
}
}

我认为问题在于 .readLine() 的使用,但我需要帮助来解决它。

【问题讨论】:

  • Err,使用Javadoc弃用声明中提到的方法?

标签: java networking client-server deprecated readline


【解决方案1】:

这里的概念问题是 DataInputStream 不是为处理文本而设计的 API。 readLine 方法对流的字符编码方案有硬性假设。

从 System.in 获取一行输入的更好方法是将其包装为 BufferedReader 并使用 BufferedReader.readLine()

如果您忽略该问题(例如使用@SuppressWarning),如果控制台配置为使用某些字符编码,您很可能会发现您的应用程序会破坏字符。例如,我怀疑 UTF-8 会损坏。

【讨论】:

  • 我在考虑这个问题,SuppressWarning 似乎不合适,因为它会像你说的那样忽略问题并且程序不会将信息传输到服务器。我试过BufferedReader.readLine(),不知道用得对不对,还是不行。
  • 如果您向我们展示了代码,其他人也许可以提供帮助。可能是您的问题与 readLine 无关。
  • 我在顶部有正常的客户端代码,我只是在 streamOut.writeUTF(console.readLine()); 之前添加了变量 line=BufferedReader.readLine(console);然后我插入了streamOut.writeUTF(line)。它看起来像这样: (...) line=BufferedReader.readLine(console); streamOut.writeUTF(line); (...)
【解决方案2】:

您可以将@SuppressWarnings("deprecation") 注释添加到您的类或方法中以抑制警告。弃用通常意味着方法或类将在下一个主要版本中删除,但为了向后兼容而保留在当前版本中。通常,如果某些东西被注释为@Deprecated,那是因为已经编写了一个新方法或类,它以更简洁的方式执行相同的任务,所以我不一定建议只添加@SuppressWarnings 注释,而是寻找正确的方法来阅读您正在使用的 Java 版本中的一行。

【讨论】:

  • 对于 stop() 已弃用的警告,我使用了 interrupt() 并且它起作用了(我认为......)但有些事情告诉我我做了一些我不应该做的事情。对于这个警告(readLine()),有没有办法知道在java中读取一行的正确方法?
  • 这是一篇关于读取行的替代方法的帖子,因为您使用的方法已被弃用 stackoverflow.com/questions/5611387/…
  • 我仍然无法使用这种方法,但也许这是在正确的道路上......我会继续努力。
猜你喜欢
  • 2012-01-31
  • 2017-03-12
  • 2020-04-07
  • 2022-09-26
  • 2016-12-07
  • 2010-11-23
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多