java uses or overrides a deprecated API error

依然范特西╮ 提交于 2020-03-21 19:52:05

问题


Hello so this is my code, somehow I still get error down there I described error, any help would be appreciated. I am not really expert with import of this and especially API itself

   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

Hello this is my code and when I try to compile it I get error saying:

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

Thanks for help :)


回答1:


Those are not errors, they are warning, your code compiled.

To explain these lines :

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

You are doing a call of DataInputStream#readLine which is deprecated since JDK 1.1 as per the documentation :

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

As for the second line :

Note: Recompile with -Xlint:deprecation for details.

It simply tells you the option to use when compiling to get more details about where you are using deprecated stuff.

Edit :

As per your comment, here how your code would looks like :

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



回答2:


From the DataInputStream API documentation:

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

So as you can see, in line String s = dis.readLine() in your code, you use a deprecated method. This means that there is a better option for doing what you're doing (as highlighted above). Even though the method is deprecated, it likely will work in some cases. But it isn't guaranteed to fulfill its contract anymore, and therefore it is better to use the similar but consistent BufferedReader.readLine() method.



来源:https://stackoverflow.com/questions/31762417/java-uses-or-overrides-a-deprecated-api-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!