当前位置: 技术问答>java相关
在线等待!!!使用或覆盖一个不鼓励使用的API。
来源: 互联网 发布时间:2015-04-17
本文导语: import java.net.*; import java.awt.*; import java.awt.event.*; public class Get extends Frame implements ActionListener { TextArea getText=new TextArea(); Button bt=new Button(" Exit "); public Get() { super("Get Data"); setLayout(new...
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Get extends Frame implements ActionListener
{
TextArea getText=new TextArea();
Button bt=new Button(" Exit ");
public Get()
{
super("Get Data");
setLayout(new FlowLayout());
setSize(450,280);
setLocation(100,100);
add(getText);
add(bt);
getText.setText("");
setVisible(true);
bt.addActionListener(this);
addWindowListener(new koWindowListener());;
}
void waitforData()
{
try
{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(5000);
while(true)
{
socket.receive(packet);
String s=new String(buffer,0,0,packet.getLength());
getText.append(s+"/n");
packet=new DatagramPacket(buffer,buffer.length);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
class koWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
public static void main(String args[])
{
new Get();
}
}
编译以后给出的信息:
C:Program FilesXinox SoftwareJCreator ProMyProjectsdatagramedatagrameGet.java 使用或覆盖一个不鼓励使用的API。
注意:
使用-deprecation重新编译,以得到详细信息。
这是什么意思?我使用了哪个api引起的?
import java.awt.*;
import java.awt.event.*;
public class Get extends Frame implements ActionListener
{
TextArea getText=new TextArea();
Button bt=new Button(" Exit ");
public Get()
{
super("Get Data");
setLayout(new FlowLayout());
setSize(450,280);
setLocation(100,100);
add(getText);
add(bt);
getText.setText("");
setVisible(true);
bt.addActionListener(this);
addWindowListener(new koWindowListener());;
}
void waitforData()
{
try
{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(5000);
while(true)
{
socket.receive(packet);
String s=new String(buffer,0,0,packet.getLength());
getText.append(s+"/n");
packet=new DatagramPacket(buffer,buffer.length);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
class koWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
public static void main(String args[])
{
new Get();
}
}
编译以后给出的信息:
C:Program FilesXinox SoftwareJCreator ProMyProjectsdatagramedatagrameGet.java 使用或覆盖一个不鼓励使用的API。
注意:
使用-deprecation重新编译,以得到详细信息。
这是什么意思?我使用了哪个api引起的?
|
//String s=new String(buffer,0,0,packet.getLength());
String(byte[] ascii, int hibyte, int offset, int count)
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a character-encoding name or that use the platform's default encoding.
String(byte[] ascii, int hibyte, int offset, int count)
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a character-encoding name or that use the platform's default encoding.
|
改成如下:
public String(byte[] bytes,
int offset,
int length,
String charsetName)
throws UnsupportedEncodingException
public String(byte[] bytes,
int offset,
int length,
String charsetName)
throws UnsupportedEncodingException
|
你可以javac -deprecation ...重新编译一次,它就会告诉你那个函数不鼓励使用
|
没有什么的
|
javac -deprecation...查出来后,现在用的JDK中肯定有新的API可以替换掉不鼓励使用的API。为了以后的版本问题还是用新的API为好。
|
建议你以后碰到这种问题,去查最新JDK文档,找到对应的被deprecated的方法,那个方法的说明里就会用代替这个旧方法的新方法介绍
|
没有什么的
|
String s=new String(buffer,0,packet.getLength(),"ISO-8859-1");
|
deprecated是已有替代品在今后有可能不在支持的方法,不过有时又不能完全避免
对这些方法的使用(例子请见Thinking in Java)
对这些方法的使用(例子请见Thinking in Java)