Question : Java - dataStreamInput.readLine decaprecated

OK so somewhere in this code the readLine is decaprected. I tried following the Java API/ Javadocs but that led me nowhere... :\

Help? Here is the code that is decaprecated:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
C:\Users\Dan\Documents\DanJavaGen>javac -Xlint:deprecation Server.java
Server.java:70: warning: [deprecation] readLine() in java.io.DataInputStream has
 been deprecated
        name = is.readLine();
                 ^
Server.java:76: warning: [deprecation] readLine() in java.io.DataInputStream has
 been deprecated
        line = is.readLine();
                 ^
2 warnings


Here is code:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
import java.io.*;
import java.net.*;

public class Server{

    static  Socket clientSocket = null;
    static  ServerSocket serverSocket = null;

    // This chat server can accept up to 10 clients' connections

    static  clientThread t[] = new clientThread[10];

    public static void main(String args[]) {

    // The default port

    int port_number=2222;

    if (args.length < 1)
        {
        System.out.println("Usage: java MultiThreadChatServer \n"+
                   "Now using port number="+port_number);
        } else {
        port_number=Integer.valueOf(args[0]).intValue();
        }

        try {
        serverSocket = new ServerSocket(port_number);
        }
        catch (IOException e)
        {System.out.println(e);}

    while(true){
        try {
        clientSocket = serverSocket.accept();
        for(int i=0; i<=9; i++){
            if(t[i]==null)
            {
                (t[i] = new clientThread(clientSocket,t)).start();
                break;
            }
        }
        }
        catch (IOException e) {
        System.out.println(e);}
    }
    }
}

class clientThread extends Thread{

    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;
    clientThread t[];

    public clientThread(Socket clientSocket, clientThread[] t){
    this.clientSocket=clientSocket;
        this.t=t;
    }

    public void run()
    {
    String line;
        String name;
    try{
        is = new DataInputStream(clientSocket.getInputStream());
        os = new PrintStream(clientSocket.getOutputStream());
        os.println("Enter your name.");
        name = is.readLine();
        os.println("Hello "+name+" to our chat room.\nTo leave enter /quit in a new line");
        for(int i=0; i<=9; i++)
        if (t[i]!=null && t[i]!=this)
            t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
        while (true) {
        line = is.readLine();
                if(line.startsWith("/quit")) break;
        for(int i=0; i<=9; i++)
            if (t[i]!=null)  t[i].os.println("<"+name+"> "+line);
        }
        for(int i=0; i<=9; i++)
        if (t[i]!=null && t[i]!=this)
            t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );

        os.println("*** Bye "+name+" ***");

        for(int i=0; i<=9; i++)
        if (t[i]==this) t[i]=null;

        is.close();
        os.close();
        clientSocket.close();
    }
    catch(IOException e){};
    }
}

Answer : Java - dataStreamInput.readLine decaprecated

you need to use a BufferedReader
Random Solutions  
 
programming4us programming4us