จะแก้ไขคำเตือนที่เลิกใช้งาน readLine () ใน ChatClient.java ได้อย่างไร

ฉันกำลังพยายามสร้างไคลเอนต์แชท/เครือข่ายเซิร์ฟเวอร์ และฉันคิดว่ามันทำงานได้ดีสำหรับเซิร์ฟเวอร์ แต่ไคลเอนต์มีปัญหาบางอย่าง เนื่องจากมีคำเตือนที่เลิกใช้แล้ว จากนั้นฉันก็บอกให้ฉันใช้แฟล็ก -Xlint และ ฉันรัน javac -Xlint:deprecated ChatClient แต่ฉันไม่รู้ว่าต้องทำอย่างไรเพื่อซ่อมแซมข้อความเตือนนี้:

ChatClient.java:47: คำเตือน: [deprecation] readLine() ใน DataInputStream เลิกใช้แล้ว 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() แต่ฉันต้องการความช่วยเหลือในการแก้ไข


person user3097315    schedule 12.12.2013    source แหล่งที่มา
comment
เอ่อ ใช้เมธอดที่กล่าวถึงในคำสั่งเลิกใช้ Javadoc หรือไม่   -  person user207421    schedule 13.12.2013


คำตอบ (2)


ปัญหาเชิงแนวคิดที่นี่คือ DataInputStream ไม่ใช่ API ที่ออกแบบมาสำหรับการประมวลผลข้อความ เมธอด readLine มีสมมติฐานแบบมีสายเกี่ยวกับรูปแบบการเข้ารหัสอักขระของสตรีม

วิธีที่ดีกว่าในบรรทัดอินพุตจาก System.in คือการล้อมเป็น BufferedReader และใช้ BufferedReader.readLine()

หากคุณเพิกเฉยต่อปัญหา (เช่น การใช้ @SuppressWarning) คุณจะต้องพบว่าแอปพลิเคชันของคุณมีปัญหากับอักขระ หากคอนโซลได้รับการกำหนดค่าให้ใช้การเข้ารหัสอักขระบางตัว ฉันสงสัยว่า UTF-8 จะพัง เป็นต้น

person Stephen C    schedule 12.12.2013
comment
ฉันกำลังคิดถึงปัญหานั้น SuppressWarning ดูเหมือนจะไม่เหมาะสมเพราะมันจะเพิกเฉยต่อปัญหาอย่างที่คุณพูดและโปรแกรมไม่ถ่ายโอนข้อมูลไปยังเซิร์ฟเวอร์ ฉันลองใช้ BufferedReader.readLine() แล้ว ฉันไม่รู้ว่าใช้ถูกต้องหรือเปล่า แต่ก็ยังใช้งานไม่ได้ - person user3097315; 13.12.2013
comment
หากคุณแสดงรหัสให้เราดู อาจมีคนอื่นสามารถช่วยได้ อาจเป็นเพราะปัญหาของคุณไม่เกี่ยวข้องกับ readLine - person Stephen C; 13.12.2013
comment
ฉันมีรหัสไคลเอนต์ปกติในส่วนบน ฉันเพิ่งเพิ่มก่อน streamOut.writeUTF(console.readLine()); เส้นตัวแปร = BufferedReader.readLine (คอนโซล); จากนั้นฉันก็แทรก streamOut.writeUTF(line) ซึ่งจะมีลักษณะดังนี้: (...) line=BufferedReader.readLine(console); streamOut.writeUTF (บรรทัด); (...) - person user3097315; 14.12.2013

คุณสามารถเพิ่มคำอธิบายประกอบ @SuppressWarnings("deprecation") ให้กับคลาสหรือเมธอดของคุณเพื่อระงับคำเตือน โดยทั่วไปการเลิกใช้งานหมายถึงวิธีการหรือคลาสจะถูกลบออกในรุ่นหลักถัดไป แต่จะถูกเก็บไว้ในรุ่นปัจจุบันเพื่อความเข้ากันได้แบบย้อนหลัง โดยปกติหากมีบางสิ่งที่มีคำอธิบายประกอบเป็น @Deprecated เป็นเพราะว่ามีการเขียนวิธีการหรือคลาสใหม่ที่ทำงานเดียวกันในลักษณะที่สะอาดกว่า ดังนั้นฉันไม่จำเป็นต้องแนะนำให้เพิ่มคำอธิบายประกอบ @SuppressWarnings แต่มองหาวิธีที่ถูกต้องในการอ่านบรรทัดใน เวอร์ชันของ Java ที่คุณใช้

person drembert    schedule 12.12.2013
comment
สำหรับคำเตือนที่เลิกใช้แล้ว stop() ฉันใช้ขัดจังหวะ () และมันใช้งานได้ (ฉันคิดว่า...) แต่มีบางอย่างบอกฉันว่าฉันได้ทำสิ่งที่ฉันไม่ควรทำ สำหรับคำเตือนนี้ (readLine()) มีวิธีทราบวิธีที่ถูกต้องในการอ่านบรรทัดใน java หรือไม่? - person user3097315; 13.12.2013
comment
นี่คือโพสต์เกี่ยวกับวิธีการอื่นในการอ่านบรรทัดในขณะนี้ซึ่งวิธีการที่คุณใช้นั้นเลิกใช้แล้ว stackoverflow.com/questions/5611387/ - person drembert; 13.12.2013
comment
ฉันยังไม่สามารถใช้งานวิธีนี้ได้ แต่บางทีนั่นอาจเป็นเส้นทางที่ถูกต้อง... ฉันจะพยายามต่อไป - person user3097315; 13.12.2013