Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不推荐使用DataInputStream类型中的方法readLine()_Java_User Input_Deprecated_Readline_Datainputstream - Fatal编程技术网

Java 不推荐使用DataInputStream类型中的方法readLine()

Java 不推荐使用DataInputStream类型中的方法readLine(),java,user-input,deprecated,readline,datainputstream,Java,User Input,Deprecated,Readline,Datainputstream,我有这个聊天客户端的Java代码 其中DataInputStream的定义如下: private static DataInputStream is=null; 我在这一部分不断出错: public void run() { /* * Keep on reading from the socket of the server */ String responseLine; //used to implement reading from the socket try{

我有这个聊天客户端的Java代码 其中DataInputStream的定义如下:

private static DataInputStream is=null;
我在这一部分不断出错:

public void run() {
/*
 * Keep on reading from the socket of the server    
 */
 String responseLine; //used to implement reading from the socket
 try{
     while((responseLine=is.readLine())!=null){ //when we recieve messages we print out here
         System.out.println(responseLine); // "is" is the object of data input stream
     }
     closed=true;
 }catch(IOException e){
     System.out.println("IOException: "+e);
 }


}
每次我尝试使用javadoc上的信息修复它时:

Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:

DataInputStream d = new DataInputStream(in);
与:

我在这一行的代码中发现另一个错误:

 is=new DataInputStream(clientSocket.getInputStream());
我怎样才能解决这个问题?如果能在这件事上得到任何帮助,我将不胜感激

类客户端代码:

公共类Clientt实现可运行的{//首先我们实现聊天客户端

//the client socket        //sience we have multiple clients we are implementing threading in our application 
private static Socket clientSocket=null;
//the output stream
private static PrintStream os=null; //initializing the input & output streams
//the input stream
private static DataInputStream is=null;

private static BufferedReader inputLine=null;
private static boolean closed=false;
public static void main(String[]args)
{
    //the default port
    int portNumber=5000;
    //the default host
    String host="localhost";

    if(args.length<2)
    {
        System.out.println("Usage:Java Client <host> <portNumber>\n"
                +"now using host="+host+",portNumber="+portNumber);
    }
    else
    {
        host=args[0];
        portNumber=Integer.valueOf(args[1]).intValue();
    }

    /* 
     * Open a socket on a given host and port.
     * Open input and Output streams
     */

    try{
        clientSocket=new Socket(host,portNumber); //initializing clientsocket with mentioned host and port num
        inputLine=new BufferedReader(new InputStreamReader(System.in));//read input from the user using inputLine
        os=new PrintStream(clientSocket.getOutputStream());//outputstream is used to write to socket
        is=new DataInputStream(clientSocket.getInputStream());// inputstream is uset to read from socket
    }catch(UnknownHostException e){
        System.err.println("Don't know about host"+host);
    }catch(IOException e) {
        System.err.println("Couldn't get I/O for connection to the host"+host);
      }
    /*
     * If everything has been initialized then we want to write some date 
     * to the socket we have opened a connection to on the port portNumber
     */

    if(clientSocket!=null && os!=null && is!=null)
    {
        try{
            //Creat a thread to read from the server
            new Thread(new Clientt()).start(); //here we implement threading
            while(!closed)
            {
                os.println(inputLine.readLine());//loop continues infinetely untill we terminate application and writes to socket using output stream object
            }
            /*
             * Close the output stream,close the input stream,close the socket
             */
            os.close();
            is.close();
            clientSocket.close();
        }catch(IOException e)
        {
            System.out.println("IOException: "+e);

        }
    }

}

/*
 * Creat thread to read from the server
 * 
 */

//@Override

public void run() {
/*
 * Keep on reading from the socket of the server    
 */
 String responseLine; //used to implement reading from the socket
 try{
     while((responseLine=is.readLine())!=null){ //when we recieve messages we print out here
         System.out.println(responseLine); // "is" is the object of data input stream
     }
     closed=true;
 }catch(IOException e){
     System.out.println("IOException: "+e);
 }


}
//客户端套接字//因为我们有多个客户端,所以我们正在应用程序中实现线程
私有静态套接字clientSocket=null;
//输出流
私有静态PrintStream os=null;//初始化输入和输出流
//输入流
私有静态DataInputStream为空;
私有静态BufferedReader inputLine=null;
私有静态布尔闭合=false;
公共静态void main(字符串[]args)
{
//默认端口
int端口号=5000;
//默认主机
String host=“localhost”;

如果(args.length您似乎很难正确应用
BufferedReader
而不是
DataInputStream
。您需要使用
InputStreamReader
BufferedReader
和原始输入流粘合在一起

尝试以下更改:

首先是变量定义:

//private static DataInputStream is=null;
private static BufferedReader br=null;
然后,实例化:

//is=new DataInputStream(clientSocket.getInputStream());//...
br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

最后,无论
在哪里,基本上都会改为
br
(因为其他使用的方法,如
close()
readLine()
都存在于
DataInputStream
BufferedReader
中).

您可以在初始化DataInputStream时发布错误。理想情况下,您要做的是将此流传递到BufferedReader,然后使用.lines()命令方法获取所有内容。这样,您将避免使用循环来迭代每一个。关于示例代码,您提到变量
d
in
is
,但不清楚它们是如何协同工作的。我想
d
is
可能是相同的?您能为我们澄清一下吗?还有完整的类文件将有助于了解整个情况。@我已经用代码编辑了这篇文章,希望这有意义now@Aris我得到了“DataInputStream类型中的方法readLine()已弃用”,这一行根本无法工作,因此我无法让程序工作properly@Aris是流(由
行()返回)是否要立即在这些行上循环(即,作为输入)?如果它在执行循环之前等待EOF,它对聊天软件没有多大用处。我是否应该保留它
responseLine=br.readLine())!=null
?我不会再收到错误,但出于某种原因,它仍然无法工作是的,
br.readLine()
是与
is.readLine()
相对应的非弃用版本。关于为什么它不起作用,您必须自己调试…我想您现在谈论的是运行程序(与您最初问题中的编译相比)。作为记录:我使用
netcat
作为对应程序尝试了该程序,但效果很好:我可以使用您的程序发送和接收文本。
//is=new DataInputStream(clientSocket.getInputStream());//...
br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));