java 使用或覆盖已弃用的 API 错误

java uses or overrides a deprecated API error

你好,这是我的代码,不知何故,我仍然在那里遇到错误,我描述了错误,我们将不胜感激。我不是真正的进口专家,尤其是 API 本身

   import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    public class MyClass {

        public static void main(String[] args) throws IOException {
            File file = new File("Sinatra.txt");
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            DataInputStream dis = new DataInputStream(bis);

            if (dis.available() != 0) {
                // Get the line.
                String s = dis.readLine();
                // Put words to array.
                String[] sParts = s.split(" ");
                // Initialize word longest length.
                int longestLength = 1;
                for (String strx : sParts) { // Go through each sPart, the next one is called strx
                    // If the document has word longer than.
                    if (longestLength < strx.length())
                        // Set new value for longest length.
                        longestLength = strx.length();
                }
                // Because array index from "0".
                int[] counts = new int[longestLength + 1];
                for (String str : sParts) {
                    // Add one to the number of words that length has
                    counts[str.length()] += 1;
                }
                // We use this type of loop since we need the length.
                for (int i = 1; i < counts.length; i++) {
                    System.out.println(i + " letter words: " + counts[i]);
                }
            }
        }
    }

    // Result:
    //        1 letter words: 0
    //        2 letter words: 2
    //        3 letter words: 0
    //        4 letter words: 1
    //        5 letter words: 0
    //        6 letter words: 2
    //        7 letter words: 2
    //        8 letter words: 0
    //        9 letter words: 3

你好,这是我的代码,当我尝试编译它时,我得到了 错误说:

Note: MyClass.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

感谢您的帮助:)

这些不是错误,它们是警告,您的代码已编译。

解释这些行:

Note: MyClass.java uses or overrides a deprecated API.

您正在调用 DataInputStream#readLine,根据文档,自 JDK 1.1 起已弃用:

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));

至于第二行:

Note: Recompile with -Xlint:deprecation for details.

它只是告诉您在编译时使用的选项,以获取有关您在何处使用已弃用内容的更多详细信息。

编辑:

根据您的评论,您的代码如下所示:

import java.io.InputStreamReader;//Add these two imports
import java.io.BufferedReader;
...
BufferedReader br = new BufferedReader(new InputStreamReader(bis));//Use BufferedReader as suggested by the doc instead of DataInputStream
...
String s = br.readLine();//Read with the non-deprecated readLine of BufferedReader

来自 DataInputStream API 文档:

readLine()

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);

with: BufferedReader d = new BufferedReader(new InputStreamReader(in));

正如您所见,在代码的第 String s = dis.readLine() 行中,您使用了一个已弃用的方法。这意味着有一个更好的选择来做你正在做的事情(如上所述)。尽管该方法已被弃用,但它在某些情况下可能会起作用。但它不再保证履行其合同,因此最好使用类似但一致的 BufferedReader.readLine() 方法。

如果出现此警告,您只需卸载当前的 jdk 并从环境变量中删除其路径。并安装jdk版本8(8u301)并在环境变量中再次设置路径。

这个方法解决了我的问题