DataInputStream 类型的 readLine() 方法已弃用
method readLine() from the type DataInputStream is deprecated
我有这个 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);
与:
BufferedReader d = new BufferedReader(new InputStreamReader(in));
我在这一行的代码中遇到另一个错误:
is=new DataInputStream(clientSocket.getInputStream());
我该如何解决这个问题?如果我能在这件事上得到任何帮助,我将不胜感激
Class客户端代码:
public classClientt实现Runnable{//首先我们实现聊天客户端
//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);
}
}
}
您似乎很难正确应用 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()));
最后,无论你有 is
,你基本上都会更改为 br
(因为其他使用的方法,如 close()
和 readLine()
都存在于 DataInputStream
和 BufferedReader
).
我有这个 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);
与:
BufferedReader d = new BufferedReader(new InputStreamReader(in));
我在这一行的代码中遇到另一个错误:
is=new DataInputStream(clientSocket.getInputStream());
我该如何解决这个问题?如果我能在这件事上得到任何帮助,我将不胜感激
Class客户端代码:
public classClientt实现Runnable{//首先我们实现聊天客户端
//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);
}
}
}
您似乎很难正确应用 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()));
最后,无论你有 is
,你基本上都会更改为 br
(因为其他使用的方法,如 close()
和 readLine()
都存在于 DataInputStream
和 BufferedReader
).