2014年度10佳番号剧情:Student.java 使用或覆盖了已过时的 API。

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/27 23:43:01
最近刚迷上java就碰到了这个问题编译是没什么问题.
import java.io.*;
//import java.awt.*;
import java.util.*;

class Student
{
public String newStudent()
{
File f1=new File ("F:/data.txt");

if(f1.exists())
{
try
{
FileInputStream fis=new FileInputStream(f1);
DataInputStream dis=new DataInputStream(fis);
while(dis.available()>0)
{
String str=dis.readLine();
System.out.println(str);
}
System.out.println("读出完毕");
fis.close();
}
catch(Exception e)
{
System.out.println("Error:"+e.toString());
}
return "读出完毕" ;
}

try
{
System.out.println("请输入学生的信息:(以End回车结束)");
DataInputStream dis=new DataInputStream(System.in);
Vector theVector=new Vector();
String str=dis.readLine();
while (!str.equalsIgnoreCase("END"))
{
theVector.addElement(str);
str=dis.readLine();
}
FileOutputStream fos=new FileOutputStream(f1);
for(int i=0;i<theVector.size();i++)
{
fos.write(theVector.elementAt(i).toString().getBytes());
fos.write(13);fos.write(10);
}
fos.close();
System.out.println("写入完毕");
}
catch (Exception e)
{
System.out.println(e.toString());
}
return "ok";
}
public static void main(String args[])
{
//String s=newStudent();
Student FRWC=new Student();
FRWC.newStudent();
}

}

来个高手帮帮忙哈```

注意:Student.java 使用或覆盖了已过时的 API。
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
注意:Student.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。

 
 
 
不使用或覆盖已过时的 API 的好处是:
a)能提高程序在未来的 Java 平台上成功运行的几率,因为已过时的 API 将来很可能不再被支持。
b)程序可能更稳定、更安全、更高性能、更易懂、更易维护,因为 Java 里总有能代替每一个已过时的 API 的更好的 API 或技巧(详情全在 API 文档里。用 -Xlint:deprecation 重新编译即能查出所有代码里使用或覆盖的已过时的 API)。

得不到上述的好处不是很大的问题,所以对于 deprecation 编译器只警告不报错。
当然,完美的编译是最好的,且一般不难。以你的 Student.java 为例:

1)用 -Xlint:deprecation 重新编译:
javac -Xlint:deprecation Student.java

你将看到:
Student.java:19: 警告:[deprecation] java.io.DataInputStream 中的 readLine() 已过时
String str=dis.readLine();
              ^
Student.java:39: 警告:[deprecation] java.io.DataInputStream 中的 readLine() 已过时
String str=dis.readLine();
              ^
Student.java:43: 警告:[deprecation] java.io.DataInputStream 中的 readLine() 已过时
str=dis.readLine();
       ^

2)有了那么针对性的信息即能轻易地获取 API 文档中的建议。
以这个页面上的文档为例:http://shorterlink.com/?RFPWC9
当中的建议是改用 BufferedReader.readLine( )。
做了相关方面的修改的代码如下(被替代的代码被包在 /* 和 */ 里):

import java.io.*;
import java.util.*;

class Student {
    public String newStudent() {
        File f1 = new File("F:/data.txt");
        if (f1.exists()) {
            try {
                FileInputStream fis = new FileInputStream(f1);
                /* DataInputStream dis = new DataInputStream(fis);
                while (dis.available() > 0) {
                    String str = dis.readLine();
                    System.out.println(str);
                }*/
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                for (String s; (s = br.readLine()) != null; )
                    System.out.println(s);
                System.out.println("读出完毕");
                fis.close();
            } catch (Exception e) {
                System.out.println("Error:" + e.toString());
            }
            return "读出完毕";
        }
        try {
            System.out.println("请输入学生的信息:(以End回车结束)");
            /*DataInputStream dis = new DataInputStream(System.in);*/
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            Vector theVector = new Vector();
            /*String str = dis.readLine();
            while (!str.equalsIgnoreCase("END")) {
                theVector.addElement(str);
                str = dis.readLine();
            }*/
            for (String s = br.readLine(); ! s.equalsIgnoreCase("END"); s = br.readLine())
                theVector.add(s);
            FileOutputStream fos = new FileOutputStream(f1);
            for (int i = 0; i < theVector.size(); i++) {
                fos.write(theVector.elementAt(i).toString().getBytes());
                fos.write(13);
                fos.write(10);
            }
            fos.close();
            System.out.println("写入完毕");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return "ok";
    }
    public static void main(String args[]) {
        // String s=newStudent();
        Student FRWC = new Student();
        FRWC.newStudent();
    }
}
 
 
 

这个很正常啊,我们经常会碰到这种情况,只要编译通过,你能用就可以了.不用再去”请使用 -Xlint:deprecation 重新编译。”

你这个里面使用了过时的api,java每发布一个版本都要对原版本进行修改,有一些方法进行了修改但为了兼容,原方法保存了下来,但是你用的话,就会出现-Xlint:deprecation ,

你用的是JDK1.5.0对吧,,如果用jdk1.4.2就不会这样,这可能是java编码规则的原因.